Skip to content

Instantly share code, notes, and snippets.

@iamnaran
Created September 25, 2018 14:48
Show Gist options
  • Save iamnaran/be67eb37315098b3eddbfec392c27568 to your computer and use it in GitHub Desktop.
Save iamnaran/be67eb37315098b3eddbfec392c27568 to your computer and use it in GitHub Desktop.
Retrofit Callback - Customization
import android.support.annotation.NonNull;
import com.ingrails.nepatop.R;
import com.ingrails.nepatop.utils.NepaTopApplication;
import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by NaRan on 27,July,2018.
* Copyright (c) inGrails Pvt. Ltd. All rights reserved.
* [email protected]
* MacBook
**/
public class MyCallback<T> implements Callback<T> {
private String cause;
@Override
public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
}
@Override
public void onFailure(@NonNull Call<T> call, @NonNull Throwable throwable) {
if (throwable instanceof UnknownHostException) {
cause = MyApplication.getAppContext().getString(R.string.no_internet_connection);
} else if (throwable instanceof InterruptedIOException) {
cause = MyApplication.getAppContext().getString(R.string.slow_internet_connection);
} else if (throwable instanceof ConnectException) {
cause = MyApplication.getAppContext().getString(R.string.cannot_connect_to_server);
} else {
cause = MyApplication.getAppContext().getString(R.string.unknown_error);
}
}
public String getCause() {
return cause;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment