spacitron
spacitron

Reputation: 2183

How to get the HTTP error code from a failed request in Retrofit 2.0.0?

I am making call using the following callback method:

Callback<PeopleList> callback = new Callback<PeopleList>() {
    @Override
    public void onResponse(Response<PeopleList> response) {
        Toast.makeText(LoginActivity.this,getString(R.string.login_failed), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(Throwable t) {
        Toast.makeText(LoginActivity.this,getString(R.string.login_failed), Toast.LENGTH_SHORT).show();
    }
};

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://example.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
retrofit.create(MyService.class).getPeopleData().enqueue(callback);

To the following interface:

public interface MyService {

    @Headers("Accept: application/json")
    @GET("/data/people/")
    Call<PeopleList> getPeopleData();
}

This callback works just fine on successful requests. On unsuccessful ones however it does not give me the opportunity to investigate further as the onFailure method does not allow me to retrieve the http error code that came with the response.

On investigating further I found that according to several stackoverflow threads, the onResponse method should be called even on unsuccessful requests. This however seems to be at odds not only with my personal experience but also with the documentation of the Callback interface, which states that:

Communicates responses from a server or offline requests. One and only one method will be invoked in response to a given request.

So the question is, how do I get the HTTP error code from a failed response if the onResponse method isn't called?

Upvotes: 3

Views: 4098

Answers (2)

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

onResponse() will be always called, for failed requests .body() is null. response.isSuccess() is used to quickly distinguish requests with http codes between 200 and 300.

If you want to access http codes you can do the following:

int htppResultCode = response.raw().code();

It accesses the raw Response object which holds information about general outcome of the request.

Upvotes: 1

Ellyk
Ellyk

Reputation: 11

I think that the onResponse method gets called even if there is a response with an Error so something like this might work(sorry if I did something wrong first attempt to answer anybody :)

 @Override
public void onResponse(Response<PeopleList> response) {
    if(response.isSuccess()){ //good http request, do something with response.body()....
         Toast.makeText(LoginActivity.this,getString(R.string.login_failed), Toast.LENGTH_SHORT).show();
    } else { //bad http response do something with error message
        try {
            Toast.makeText(LoginActivity.this,response.errorBody().string().toString(), Toast.LENGTH_SHORT).show();
        } catch (IOException e){
            //IOException caught from response.errorBody().string() method
        }
    }
}

Upvotes: 1

Related Questions