gpt4 book ai didi

Android 改造处理 onFailure() 响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:29:48 28 4
gpt4 key购买 nike

我一直在开发 Android 应用程序,我正在使用 Retrofit

如何处理 onFailure(Throwable t) 回调 NoInternetConnectionOtherError

我检查了一些关于 stackoverflow 的问题,但没有帮助,因为我正在使用 retrofit 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);
}
}

我的界面

public interface AvsCallbackInterface<T> {

void onSuccess(Call<T> call, T t);

void onError(Call<T> call, Throwable throwable);
}

最佳答案

引自 here :

When Throwable is passed to the failure, the callback is an IOException, this means that it was a network problem (socket timeout, unknown host, etc.). Any other exception means something broke either in serializing/deserializing the data or it's a configuration problem.

You can do t instanceof IOException to determine network problem and react appropriately.

A 401 (or any non-2xx response code) will actually go to the response callback, because it was a successful response even though it may not have been a successful operation on the server. You can check this in onResponse by calling response.isSuccess().

关于Android 改造处理 onFailure() 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041182/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com