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?

  • How to categorize android layout in to small,medium,large,x-large screen?
  • Problems with importing Android platform classes
  • How do I use InputType=numberDecimal with the “phone” soft keypad?
  • ListView onScroll add more items
  • How to draw a bitmap on Android using OpenGL
  • Convert Date time in “April 6th, 2012 ” format
  • Related posts:

    Running android unit tests from the command line?
    BaseGameUtils Unknown Error, onConnectionFailed: result 4
    Setting dp in decimals in Android
    android pending intent notification problem
    Buck vs Gradle, pro's and cons for Android build systems
    Promises for Android?
  • Launch android application from a browser link
  • Processing Android video frame by frame while recording
  • android autocompletetextview hint results hidden under keyboard
  • Why does Android NoSuchMethodException occurs at AlertDialog.Builder's setOnDismissListener
  • Android Email EditText Validation
  • How handler classes work in Android
  • 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.