【问题标题】:Android/Java : Problem with sending param and receiving JSON response with RetroFit in POSTAndroid/Java:在 POST 中使用 RetroFit 发送参数和接收 JSON 响应的问题
【发布时间】:2020-05-26 18:02:03
【问题描述】:

我想使用 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”和“:”之间的空格,现在我收到了该错误消息:java.io.IOException: unexpected end of stream on https://xxx.newtotelapps.fr:5000/...

【问题讨论】:

标签: java android json retrofit retrofit2


【解决方案1】:

编辑:

0x20 字符

空格字符,表示单词之间的空格,由键盘的空格键产生,由代码 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);

}

您在“Accept :”之后错误地保留了空格。执行此操作“接受:

错误解决方法: 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);

【讨论】:

  • 哦,是的,你是对的!我的错。谢谢你。我现在有另一个错误消息是“java.io.IOException:xxx.newtotelapps.fr:5000/...上的流意外结束”
  • 是的,我很高兴等待......我只需要弄清楚它
猜你喜欢
  • 2019-02-06
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多