我一直在使用 Retrofit 开发Android应用程序。
如何处理 NoInternetConnection 和 OtherError
的onFailure(Throwable t)
回调
我已经检查了一些关于stackoverflow的问题,但它没有帮助,因为我正在使用改造2
compile 'com.squareup.retrofit2:retrofit:2.1.0'
回拨代码
public class AvsCallBack<T> implements Callback<T> {
private static final String TAG = "AvsCallBack";
private AvsCallbackInterface<T> avsInterface;
private Activity activity;
private boolean validateError = true;
public AvsCallBack(Activity activity, AvsCallbackInterface<T> avsInterface) {
this.activity = activity;
this.avsInterface = avsInterface;
}
public AvsCallBack(Activity activity, AvsCallbackInterface<T> avsInterface, boolean validateError) {
this.activity = activity;
this.avsInterface = avsInterface;
this.validateError = validateError;
}
@Override
public void onResponse(Call<T> call, Response<T> response) {
if (response.isSuccessful()) {
if (BuildConfig.DEBUG) Log.d(TAG, new Gson().toJson(response.body()));
avsInterface.onSuccess(call, response.body());
} else {
onFailure(call, null);
}
}
@Override
public void onFailure(Call<T> call, Throwable t) {
if (validateError) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Retrofit Exception -> " + ((t != null && t.getMessage() != null) ? t.getMessage() : "---"));
if (t != null && (t instanceof IOException || t instanceof SocketTimeoutException || t instanceof ConnectException)) {
if (t instanceof SocketTimeoutException || t instanceof TimeoutException) {
((BaseActivity) activity).showToast("Oops something went wrong");
//avsInterface.onError(call, new AvsException("Oops something went wrong, please try again later..."));
} else {
((BaseActivity) activity).showToast("Please check your internet connection...");
//avsInterface.onError(call, new AvsException("Please check your internet connection..."));
}
} else {
((BaseActivity) activity).showToast("Oops something went wrong");
}
if (BuildConfig.DEBUG)
Log.d(TAG, "Avs Exception -> " + ((t != null && t.getMessage() != null) ? t.getMessage() : "---"));
}
avsInterface.onError(call, t);
}
}
MyInterface的
public interface AvsCallbackInterface<T> {
void onSuccess(Call<T> call, T t);
void onError(Call<T> call, Throwable throwable);
}
答案 0 :(得分:2)
引自here:
当
Throwable
传递给失败时,回调是一个IOException
,这意味着它是一个网络问题(套接字 超时,未知主机等)。任何其他例外都意味着什么 在序列化/反序列化数据时打破了它或者它是a 配置问题。您可以执行
t instanceof IOException
来确定网络问题 做出适当的反应。401(或任何非2xx响应代码)实际上将转到响应 回调,因为它是一个成功的回应,即使它可能没有 已在服务器上成功运行。你可以检查一下
onResponse
致电response.isSuccess()
。
答案 1 :(得分:2)
我所做的是使用拦截器来检查互联网连接并做出相应的响应。在我的情况下,我使用MPV,因此它会自动调用BasePresenter方法。
拦截器:
class NetworkStatusInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
if (!Util.isInternet(mContext)) {
return new Response.Builder()
.code(1007)
.request(chain.request())
.protocol(Protocol.HTTP_2)
.body(ResponseBody.create(MediaType.parse("{}"),"{}"))
.message(mContext.getString(R.string.warning_no_internet))
.build();
}
return chain.proceed(chain.request());
}
}
将拦截器附加到OkHTTPClient:
builder.addInterceptor(new NetworkStatusInterceptor());
您可以在onResponse中处理此问题:
public void onResponse(final Call<T> call, final Response<T> response) {
if (response.code() == 1007) {
// handle no internet error
}
}