How to get Retrofit success responce 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?

  • Android material design buttons - Pre lollipop
  • AsyncTaskLoader vs AsyncTask
  • Fragment in ViewPager using FragmentPagerAdapter is blank the second time it is viewed
  • Generate android apk using shell script from php?
  • Adding my custom View into an XML layout throws exception
  • android what is the difference between getAction() and getActionMasked() in MotionEvent
  • NoClassDefFoundError - Eclipse and Android
  • android: how to change layout on button click?
  • ViewPager Activity to notify a Fragment of a specific event
  • Android Jack: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Consumer
  • WebView textarea doesn't pop up the keyboard
  • Smooth scroll and Fling with NestedScrollView,AppBarLayout and CoordinatorLayout
  • 3 Solutions collect form web for “How to get Retrofit success responce 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 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 Response object in success callback as response.getStatus()

    EDIT

    I assume you are using okhttp with retrofit. ok http has a powerful tool called Interceptor

    you can catch the response before retrofits Callback and get statuscode from response like this:

    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);
            }
        });
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.