[英]Retrofit POST raw string body
I am using Retrofit to send a POST request to a server. 我正在使用Retrofit将POST请求发送到服务器。 The body of the POST must be in the form jdata={"key1":"value1",...}
along with a Content-Type header set to application/x-www-form-urlencoded
. POST的主体必须为jdata={"key1":"value1",...}
以及Content-Type标头,该标头设置为application/x-www-form-urlencoded
。 I found a similar question but the accepted answer is not working. 我发现了类似的问题,但接受的答案无效。
Here's what I tried - 这是我尝试过的-
My interface 我的介面
public interface APIHandler {
@Headers("Content-Type: application/x-www-form-urlencoded")
@FormUrlEncoded
@POST(URL)
Call<ResponseBody> getdata(@Field("jdata") String jdata);
}
Call function 通话功能
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) {
}
});
}
But I receive response = null
and status = 200
. 但是我收到response = null
, status = 200
。 What am I doing wrong? 我究竟做错了什么? The expected response is only a string and not a JSON array. 预期的响应只是一个字符串,而不是JSON数组。
I am leaving this here so that it helps someone. 我将其留在这里,以便对某人有所帮助。
The above code is correct. 上面的代码是正确的。 As I mentioned in the last line, a plain string response was expected. 正如我在最后一行中提到的那样,期望有一个纯字符串响应。 But since it is not a JSON response, the conversion probably did not work and the response was null. 但是由于它不是JSON响应,因此转换可能无法正常进行,并且响应为null。 The only solution I could find was to directly convert the response to string - 我能找到的唯一解决方案是将响应直接转换为字符串-
try {
stresp = response.body().string()
Log.d("MainActivity", "response = " + stresp + " status: " + statusCode);
} catch (IOException e) {
//Handle exception
}
There might be a better way to handle this but that worked for me! 也许有更好的方法来解决这个问题,但这对我有用!
You can use like that. 您可以那样使用。 I have tested this and it working fine 我已经对此进行了测试,并且效果很好
public interface APIHandler {
@POST(URL)
Call<ResponseBody> getdata(@Body JsonObject body);
}
Request body: 请求机构:
JsonObject requestBody = new JsonObject();
requestBody.addProperty("id", "value1");
requestBody.addProperty("id1", "value2");
Prepare call in Retrofit 2.0 在Retrofit 2.0中准备通话
APIHandler iAPI = retrofit.create(APIHandler.class);
And Call function : 和通话功能:
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) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.