I'm using Retrofit 2 in my project. I have a problem with below scenario.
When I call to API and server tell me that the request was not executed with the error code is 4xx or 500 with details message of the problem.
But seems that retrofit understands that it's an http error and did not collect the error message from Server.
I see the closely same problem here but it was not resolved my issue.
I completely get response success and I'm sure that there no problem in my implementation.
@Override
public void onResponse(Response<CustomResponse> response, Retrofit retrofit) {
Log.d("Response : ", response.body(), response.code()); // here i got response.body() is null if response code is not 200, if response code is 200 -- it work well as I expected.
onRequestSuccess(response);
}
I have a look at Response class in retrofit and I see below code:
private Response(com.squareup.okhttp.Response rawResponse, T body, ResponseBody errorBody) {
this.rawResponse = checkNotNull(rawResponse, "rawResponse == null");
this.body = body;
this.errorBody = errorBody;
}
And
public static <T> Response<T> error(ResponseBody body, com.squareup.okhttp.Response rawResponse) {
return new Response<>(rawResponse, null, body);
}
Seems that when response code from server was not 200, retrofit understand that it's error and set body as null.
So any one can help to get the "correct" value of response body in this case?