[英]Retrofit2 POST Request response returns null
I make a POST
request utilizing retrofit2 with a number of parameters.我使用带有多个参数的改造 2 发出
POST
请求。 The call is made successfully, but the response.body()
is equal to null.调用成功,但
response.body()
等于 null。 I've checked the URL with the same parameters with Postman and it works fine, so I really do not think that it is an issue with the call, but I believe the method I am using to receive the Callback is incorrect (the error code I receive from response.code()
is 500) .我已经用 Postman 检查了具有相同参数的 URL 并且它工作正常,所以我真的不认为这是调用的问题,但我相信我用来接收回调的方法不正确(错误代码我从
response.code()
收到的是 500) 。 Any help would be immensely appreciated, thank you.任何帮助将不胜感激,谢谢。
POST interface: POST接口:
public interface POSTApi {
String BASE_URL = "someURL/";
@POST("like")
Call<Resultlike> post(@Body Resultlike login);
}
Making the call打电话
private Retrofit retrofit = new Retrofit.Builder().baseUrl(POSTApi.BASE_URL)
.callbackExecutor(Executors.newSingleThreadExecutor())
.addConverterFactory(GsonConverterFactory.create())
.build();
POSTApi apiInterface = retrofit.create(POSTApi.class);
final Resultlike resultlike = new Resultlike("param1","param2","param3","param4");
Call<Resultlike> call = apiInterface.post(resultlike);
call.enqueue(new Callback<Resultlike>() {
@Override
public void onResponse(Call<Resultlike> call, Response<Resultlike> response) {
Resultlike logresponse = (Resultlike) response.body();
Log.e("TAG", String.valueOf(response.code()));
if(!response.isSuccessful()) {
Log.e("TAG", "NOT SUCCESSFUL");
}
Log.e("TAG", logresponse.getResult().getMessage());
Log.e("TAG", logresponse.getResult().getStatus());
}
@Override
public void onFailure(Call<Resultlike> call, Throwable t) {
Log.e("TAG", "FAILURE");
}
});
ResultLike Object class: ResultLike 对象类:
public class Resultlike {
// These 4 are parameters that are passed in to make the POST request
@SerializedName("uid")
public String uid;
@SerializedName("contentId")
public String contentId;
@SerializedName("like")
public String like;
@SerializedName("timestamp")
public String timestamp;
// This is supposed to be receiving the callback of the data
@SerializedName("result")
public DataLike result;
public Resultlike(String uid, String contentId, String like, String timestamp) {
this.uid = uid;
this.contentId = contentId;
this.like = like;
this.timestamp = timestamp;
}
public DataLike getResult() { return result; }
public void setResult(DataLike setData) { result = setData; }
public String getUid() { return uid; }
public String getTimestamp() { return timestamp; }
public String getLike() { return like; }
public String getContentId() { return contentId; }
public void setUid(String uid) { this.uid = uid; }
public void setTimestamp(String timestamp) { this.timestamp = timestamp; }
public void setLike(String like) { this.like = like; }
public void setContentId(String contentId) { this.contentId = contentId; }
} }
DataLike Object class : DataLike 对象类:
public class DataLike {
@SerializedName("status")
@Expose
private String status = "";
@SerializedName("message")
@Expose
private String message = "";
public String getMessage() { return message; }
public String getStatus() { return status; }
public void setMessage(String message) { this.message = message; }
public void setStatus(String status) { this.status = status; }
}
JSON Data I am expecting from the callback :我期望从回调中得到 JSON 数据:
{
"result": {
"status": "some string",
"message": "some string"
}
}
Relevant Logs before crashing occurs:发生崩溃前的相关日志:
// 500 is the response code resulting from response.code()
E/TAG: 500
E/TAG: NOT SUCCESSFUL
Web 服务响应代码是 500,因此它表示服务器端错误,因此请通知 Web 端开发人员检查他们这边的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.