Android/Java:在 POST 中使用 RetroFit 发送参数和接收 JSON 响应时出现问题

Android/Java : Problem with sending param and receiving JSON response with RetroFit in POST

我想通过 RetroFitPOST 中发送一个参数,但无法得到解决方案。

我只想传递一个 URL,带有一个键和一个值的参数,并得到类型为

的 JSON 答案
{"result":[{"id":196,"CREATION_DATE":"2020-01-22T14:33:49.000Z"}]}  

这是我的代码改造:

public interface RetrofitInterface {

    @Headers({"Content-type: application/json" , "Accept : application/json"})
    @Streaming
    @POST("https://xxx.newtotelapps.fr:5000/device/listEvent")
    Call<JSONObject> getListEvent(@Body JSONObject jsonObject);
}

我的主要代码:

                JSONObject params2 = new JSONObject();
                try {
                    params2.put("CODE_USER", UserCode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                CookieJar cookieJar2 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));

                OkHttpClient client2 = new OkHttpClient.Builder()
                        .cookieJar(cookieJar2)
                        .build();

                Retrofit retrofit3 = new Retrofit.Builder()
                        .baseUrl("https://xxx.newtotelapps.fr:5000/device/listEvent/")
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                RetrofitInterface downloadService2 = retrofit3.create(RetrofitInterface.class);

                Call<JSONObject> call2 = downloadService2.getListEvent(params2);

                call2.enqueue(new Callback<JSONObject>() {
                        @Override
                        public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                            //displaying the message from the response as toast


                            System.out.println("test ");

                        }

                    @Override
                    public void onFailure(Call<JSONObject> call, Throwable t){
                    System.out.println(t.toString());
                }
                });

我收到此错误消息:java.lang.IllegalArgumentException: Unexpected char 0x20 at 6 in header name: Accept

编辑:我抑制了 "Accept" 和“:”之间的 space,现在我收到了错误消息:java.io.IOException: unexpected end of stream on https://xxx.newtotelapps.fr:5000/...

编辑:

0x20个字符

表示单词之间 space 的 space 字符,由键盘的 space 栏产生,由代码 0x20(十六进制)表示,被认为是非打印图形(或不可见图形)而不是控制字符。

复制以下代码:

public interface RetrofitInterface {

@Headers({"Content-type: application/json" , "Accept: application/json"})
@Streaming
@POST("https://xxx.newtotelapps.fr:5000/device/listEvent")
Call<JSONObject> getListEvent(@Body JSONObject jsonObject);

}

您在“接受:”之后错误地保留了 space。这样做“接受:

错误解决方案: java.io.IOException: unexpected end of stream on https://xxx.newtotelapps.fr:5000/...

使用Google Gson库,添加到build.gradle:

dependencies {
    implementation 'com.google.code.gson:gson:2.8.6'
}

使用: import com.google.gson.JsonObject;

而不是: import org.json.JSONObject;

希望你能收到。

否则第二种情况会出现错误:

您需要在页眉中设置Connection:close

@Headers({"Content-type: application/json" , "Accept: application/json", "Connection: close"})

第三种情况也可能出现错误:

有服务器端问题...您需要在服务器端脚本

中设置header('Content-Length: '.$length);