touchingtwist touchingtwist - 1 month ago 10
Android Question

How to check response code on Android retrofit?

CODE:

Call<ResponseBody> call = postApiService.uploadFile(body, coordinatePoint, textToPost);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.isSuccessful()){
Toast.makeText(getContext(), response.code(), Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getContext(), response.code(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getContext(), getString(R.string.check_internet), Toast.LENGTH_LONG).show();
}


On this code, i think error is on
response.code()


Because error code is like
android.content.res.Resources$NotFoundException: String resource ID #0xc9


on that
Toast.makeText(getContext(), response.code(), Toast.LENGTH_LONG).show();


So, how to get response code on android retrofit2?

Please would you let me know about it?

Answer

You are getting the android.content.res.Resources$NotFoundException: String resource ID #0xc9 message because you are calling makeText(Context context, int resId, int duration) in onResponse. Here, the second parameter expects a string resource ID of type int, but you are passing a response code instead. Simply convert your response code to a String to print out the correct value:

Toast.makeText(getContext(), Integer.toString(response.code()), Toast.LENGTH_LONG).show();