1

I am using Retrofit 2.0 library in my android application by adding it into build.gradle file

// retrofit, gson
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

related code is given below

ApiInterface.java

public interface ApiInterface {
   @GET("contacts/")
   Call<ContactsModel> getContactsList();
}

ApiClient.java

public class ApiClient {
public static final String BASE_URL = "http://myexamplebaseurl/";
private static Retrofit retrofit = null;


public static Retrofit getClient() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
  }
}

MainActivity.java

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

Call<ContactsModel> call = apiService.getContactsList();
    call.enqueue(new Callback<ContactsModel>() {
        @Override
        public void onResponse(Call<ContactsModel> call, Response<ContactsModel> response) {
            if(response.isSuccessful()){
                /*here is my data handling*/
            }

        }

        @Override
        public void onFailure(Call<ContactsModel> call, Throwable t) {
            /*It is the request failure case, 
              I want to differentiate Request timeout, no internet connection and any other reason behind the request failure
            */
        }
    });

if we get status code as 4xx or 5xx even though onResponse() will called, so there we need handle that condition also.

Here my question is, How to differentiate reason for request failure i.e onFailure() by using Retrofit 2.0 in Android?

sandeepmaaram
  • 4,191
  • 2
  • 37
  • 42

1 Answers1

1

Here my question is, How to differentiate reason for request failure by using Retrofit 2.0 in Android?

if you have a 4xx or 5xx error, onResponse is still called. There you have to check the response code of the code to check if everything was fine. E.g

if (response.code() < 400) {

in case of No Network connection, onFailure is called. There you could check the instance of the throwable. Typically an IOException

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thanks for reply. I agree with your point "4xx or 5xx error, onResponse is still called" and you mean to say If that exception is IOException Type then, is it "No Network connection" ? – sandeepmaaram Jul 05 '16 at 14:52
  • indeed. Look the direct subclasses of IOException [here](https://docs.oracle.com/javase/7/docs/api/java/io/IOException.html) – Blackbelt Jul 05 '16 at 14:56
  • more than "No Network connection" I would say, there were problems reaching the server out. – Blackbelt Jul 05 '16 at 15:03
  • 1
    If the remote server is down or the device has no internet, in both situations you will get the `IOException`. how can we show the common message "No Network connection" to the user. How can we differentiate these two. – Madhu Jul 06 '16 at 09:32
  • @Madhu you have to check the instance of IOExcepiton – Blackbelt Jul 06 '16 at 09:35