How to get Retrofit success response status codes

I am not able to get success response status code from response like 200,201.. etc. As we can easily get error codes from RetrofitError class like error.isNetworkError() and error.getResponse().getStatus(). Is there any workaround for getting status codes?


As per Retrofit 2.0.2, the call is now

@Override
public void onResponse(Call<YourModel> call, Response<YourModel> response) {
    if (response.code() == 200) {
       // Do awesome stuff
    } else {
       // Handle other response codes
    }
}

Hope it helps someone :-)

EDIT: Many apps could also benefit from just checking for success (response code 200-300) in one clause, and then handling errors in other clauses, as 201 (Created) and 202 (Accepted) would probably lead to the same app logic as 200 in most cases.

@Override
public void onResponse(Call<YourModel> call, Response<YourModel> response) {
    if (response.isSuccessful()) {
       // Do awesome stuff
    } else if (response.code() == 401) {
       // Handle unauthorized
    } else {
       // Handle other responses
    }
}

You can get the status code in success() just like you do it in failure()

@Override
public void success(Object object, Response response) {
    response.getStatus() // returns status code integer
}

Since you have a Response object in success callback as response.getStatus()

EDIT

I assume you are using okhttp with retrofit.

okhttp has a powerful tool called Interceptor

You can catch the response before retrofits Callback and get status code from the response:

OkHttpClient client = new OkHttpClient();
client.interceptors().add(new  Interceptor(){
    @Override 
    public Response intercept(Chain chain) throws IOException{

    Request request = chain.request();
    Response response = chain.proceed(request);
    response.code()//status code
    return response;

});

// then add it to you Restclient like this:
restAdapter = new RestAdapter.Builder()
                .setEndpoint(URL_SERVER_ROOT)
                .setClient(new OkClient(client))  //plus your configurations       
                .build();

To learn more about interceptors visit here.


i achieved it by following codes:

public abstract class BaseCallBack<T> {

    public abstract void onSuccess(Response<T> response);
    public abstract void onFailure(Response<T> response);
}

public abstract class SuccessCallback<T> extends BaseCallBack<T> implements Callback<T>{


    @Override
    public void onResponse(Call<T> call, Response<T> response) {

        if(response.code()>= 400 && response.code() < 599){
             onFailure(response);
        }
        else {
            onSuccess(response);
        }

    }

    @Override
    public void onFailure(Call<T> call, Throwable t){

    }

    @Override
    public void onSuccess(Response<T> response) {

    }

    @Override
    public void onFailure(Response<T> response) {

    }
}

When the rest api returns 400 etc then the onFailure method will be call by default. If you wanna do something onsuccess:(when returns 200 etc)

 ServiceConnector.api.getHomePage().enqueue(new SuccessCallback<Void>() {
        @Override
        public void onSuccess(Response<Void> response) {
            super.onSuccess(response);
        }
    });