Retrofit 2 - Response body null when response status is 422 (unprocessable entity)
By default, when your server is returning an error code response.body()
is always null
. What you are looking for is response.errorBody()
. A common approach would be something like this:
@Override
public void onResponse(Response<JsonObject> response, Retrofit retrofit) {
if (response.isSuccess()) {
response.body(); // do something with this
} else {
response.errorBody(); // do something with that
}
}
If you need something advanced take a look at Interceptors and how to use them