【问题标题】:Retrofit POST raw string body改造 POST 原始字符串体
【发布时间】:2016-03-08 15:10:36
【问题描述】:

我正在使用 Retrofit 向服务器发送 POST 请求。 POST 的正文必须采用 jdata={"key1":"value1",...} 格式,并且 Content-Type 标头设置为 application/x-www-form-urlencoded。我发现了一个similar 问题,但接受的答案不起作用。

这是我尝试过的 -

我的界面

public interface APIHandler {
    @Headers("Content-Type: application/x-www-form-urlencoded")
    @FormUrlEncoded
    @POST(URL)
    Call<ResponseBody> getdata(@Field("jdata") String jdata);
}

调用函数

public void load() {
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("BASE_URL")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

// prepare call in Retrofit 2.0
APIHandler iAPI = retrofit.create(APIHandler.class);

String requestBody = "{\"id\":\"value\",\"id1\":\"value2\"}"
Call<ResponseBody> call = iAPI.getData(requestBody);

call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> c, Response<ResponseBody> response) {
        if (response.isSuccess()) {
            ResponseBody result = response.body();
            String gs = new Gson().toJson(result);
            Log.d("MainActivity", "response = " + gs + " status: " + statusCode);

        } else {
            Log.w("myApp", "Failed");
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> c, Throwable t) {
    }
});
}

但我收到了response = nullstatus = 200。我究竟做错了什么?预期的响应只是一个字符串,而不是 JSON 数组。

【问题讨论】:

  • 这个{"key1":"value1",...}是一个json数据。您可以使用 gson 将 pojo 转换为 json 对象,然后将其发布到服务器
  • 是的,但我需要发帖jdata={"key1":"value1",...} 而不是{"key1":"value1",...}
  • 参数已经是一个字符串(requestBody)。
  • 您是否通过邮递员查看回复?

标签: android retrofit2


【解决方案1】:

我把它留在这里是为了帮助别人。

上面的代码是正确的。正如我在最后一行中提到的,plain string 响应是预期的。但由于它不是 JSON 响应,因此转换可能不起作用并且响应为空。我能找到的唯一解决方案是将响应直接转换为字符串-

try {
        stresp = response.body().string()
        Log.d("MainActivity", "response = " + stresp + " status: " + statusCode);
    } catch (IOException e) {
                    //Handle exception
}

可能有更好的方法来处理这个问题,但这对我有用!

【讨论】:

    【解决方案2】:

    你可以这样使用。我已经测试过了,它工作正常

    public interface APIHandler {
        @POST(URL)
        Call<ResponseBody> getdata(@Body JsonObject body);
    }
    

    请求正文:

    JsonObject requestBody = new JsonObject();
    requestBody.addProperty("id", "value1");
    requestBody.addProperty("id1", "value2");
    

    在 Retrofit 2.0 中准备调用

    APIHandler iAPI = retrofit.create(APIHandler.class);
    

    并调用函数:

     Call<ResponseBody> call = iAPI.getData(requestBody);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> c, Response<ResponseBody> response) {
                if (response.isSuccess()) {
                    String result = response.body().string();
    
                    Log.d("MainActivity", "response = " + result);
    
                } else {
                    Log.w("myApp", "Failed");
                }
            }
    
            @Override
            public void onFailure(Call<ResponseBody> c, Throwable t) {
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多