I want to log the error message when the API call returns bad request or status code = 400
I am doing this
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if(response.isSuccessful()){
Log.e(TAG, "onResponse: "+response.message() );
Toast.makeText(RegisterCanister.this, "Registered Successfully!", Toast.LENGTH_SHORT).show();
}
else {
try {
Log.e(TAG, "onResponse: "+response.errorBody().string() );
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call call, Throwable t) {
Toast.makeText(RegisterCanister.this, "Inhaler Registration Failed", Toast.LENGTH_SHORT).show();
}
});
The above code gets into the else condition as the response code is 400 where the response.errorBody is not null but I am not able to get the response of it. I tried using a converter and also JSONObject, faced the same issue. Wasn't able to extract the info from the errorBody()
Log.e(TAG, "onResponse: "+response.errorBody().string() );
logs
E/RegisterCanister: onResponse:
but
Log.e(TAG, "onResponse: "+response.errorBody());
logs
E/RegisterCanister: onResponse: okhttp3.ResponseBody$1@a33dda6
my error response is like this when trying in postman.
{"error":"Device not found"}