Retrofit2 POST请求响应返回null

时间:2019-07-01 07:20:25

标签: java android retrofit2

我使用带有大量参数的Retrofit2发出了POST请求。呼叫已成功完成,但是response.body()等于null。我已经用Postman检查了具有相同参数的URL,并且可以正常工作,所以我真的不认为这是调用的问题,但是我认为我用来接收Callback的方法不正确(错误代码我从response.code()得到的是500)。任何帮助将不胜感激,谢谢。

POST界面:

public interface POSTApi {
  String BASE_URL = "someURL/";

  @POST("like")
  Call<Resultlike> post(@Body Resultlike login);
}

拨打电话

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对象类:

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对象类:

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数据:

{
  "result": {
    "status": "some string",
    "message": "some string"
  }
}

崩溃之前的相关日志:

// 500 is the response code resulting from response.code()
E/TAG: 500
E/TAG: NOT SUCCESSFUL

1 个答案:

答案 0 :(得分:1)

Web服务响应代码为500,因此表明服务器端错误, 因此请通知网络端开发人员从他们那里检查出什么问题。