Retrofit2 POST Request response returns null

Ed Delacruz :

I make a POST request utilizing retrofit2 with a number of parameters. The call is made successfully, but the response.body() is equal to 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) . Any help would be immensely appreciated, thank you.

POST interface:

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:

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 :

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 :

{
  "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
Pradip Vadher :

web service response code is 500, so it indicate server side error, so inform web-end side developer to check whats wrong from their side.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=155543&siteId=1