David Foie Gras
David Foie Gras

Reputation: 2080

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?

Upvotes: 1

Views: 398

Answers (2)

Jacob
Jacob

Reputation: 523

The Toast.makeText method, which you are using, is overloaded and using a string resource int as second argument.

Consider passing your second argument wrapped by String.valueOf(response.code()).

Upvotes: 1

cascal
cascal

Reputation: 3019

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();

Upvotes: 1

Related Questions