Created
September 25, 2018 14:48
-
-
Save iamnaran/be67eb37315098b3eddbfec392c27568 to your computer and use it in GitHub Desktop.
Retrofit Callback - Customization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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