本文已经废弃。请读者移步到
Android使用rxjava封装重试处理工具类 - 简书
我们在做一些异步请求时,为了确保请求成功需要进行重试。
那么我们需要灵活的配置重试间隔时间,重试次数, 还需要写个倒计时代码。
为此,本文使用rxjava为基础封装了一个工具类。
其中要注意的是内存泄漏问题,为此使用rxjava的周期组件AndroidLifecycleScopeProvider进行处理,如果非全局上下文需要在构造方法传入LifecycleOwner类。
下面直接上代码:
public class RetryForTimeDownHelper {
private static final String TAG = "RetryForTimeDownHelper";
/**
* 重试次数
*/
private volatile int retryCount;
/**
* 最大重试次数
*/
private int maxRetryCount;
/**
* 重试时间
*/
private long retryTimeMilli;
private OnRetryCallBack onRetryCallBack;
private WeakReference<LifecycleOwner> lifecycleOwnerWeakReference;
/**
* @param lifecycleOwner 非全局上下文需要传入,防止内存泄漏
* @param maxRetryCount 最多重试次数
* @param retryTimeMilli 重试间隔时间 单位毫秒
* @param onRetryCallBack 重试回调:重试、重试结束处理结果
*/
public RetryForTimeDownHelper(LifecycleOwner lifecycleOwner, int maxRetryCount, long retryTimeMilli, OnRetryCallBack onRetryCallBack) {
if (lifecycleOwner != null) {
this.lifecycleOwnerWeakReference = new WeakReference<>(lifecycleOwner);
}
this.maxRetryCount = maxRetryCount;
this.retryTimeMilli = retryTimeMilli;
this.onRetryCallBack = onRetryCallBack;
}
public RetryForTimeDownHelper(int maxRetryCount, long retryTimeMilli, OnRetryCallBack onRetryCallBack) {
this(null, maxRetryCount, retryTimeMilli, onRetryCallBack);
}
public int getRetryCount() {
return retryCount;
}
public void retry(FailBean failBean) {
retryCount++;
if (retryCount > maxRetryCount) {
if (onRetryCallBack != null) {
onRetryCallBack.onComplete(failBean);
}
return;
}
//非全局上下文需要传入,防止内存泄漏
if (lifecycleOwnerWeakReference != null) {
LifecycleOwner lifecycleOwner = lifecycleOwnerWeakReference.get();
if (lifecycleOwner != null) {
timeDownRetryLifecycle(lifecycleOwner);
}
return;
}
//全局上下文中
timeDownRetry();
}
private void timeDownRetry() {
//倒计时3秒 重试
Observable.timer(retryTimeMilli, TimeUnit.MILLISECONDS)
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(@NonNull Disposable disposable) {
}
@Override
public void onNext(@NonNull Long number) {
if (onRetryCallBack != null) {
onRetryCallBack.onNext();
}
}
@Override
public void onError(@NonNull Throwable throwable) {
if (throwable != null) {
Log.e(TAG, "onError: ", throwable);
}
}
@Override
public void onComplete() {
}
});
}
private void timeDownRetryLifecycle(LifecycleOwner lifecycleOwner) {
//倒计时3秒 重试
Observable.timer(retryTimeMilli, TimeUnit.MILLISECONDS)
//AutoDispose 防止内存泄漏
.as(AutoDispose.<Long>autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner)))
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(@NonNull Disposable disposable) {
}
@Override
public void onNext(@NonNull Long number) {
if (onRetryCallBack != null) {
onRetryCallBack.onNext();
}
}
@Override
public void onError(@NonNull Throwable throwable) {
if (throwable != null) {
Log.e(TAG, "onError: ", throwable);
}
}
@Override
public void onComplete() {
}
});
}
public interface OnRetryCallBack {
/**
* 调用重试
*/
void onNext();
/**
* 处理结果
*/
void onComplete(FailBean failBean);
}
public static class FailBean {
private String errorCodeStr;
private String errorMsgStr;
public String getErrorCodeStr() {
return errorCodeStr;
}
public void setErrorCodeStr(String errorCodeStr) {
this.errorCodeStr = errorCodeStr;
}
public String getErrorMsgStr() {
return errorMsgStr;
}
public void setErrorMsgStr(String errorMsgStr) {
this.errorMsgStr = errorMsgStr;
}
}
}
使用步骤
我们以登录为例
1、在调用登录时构造对象
private volatile RetryForTimeDownHelper retryForTimeDownHelper;
public void login(){
//重试3次,间隔3秒重试
retryForTimeDownHelper = new RetryForTimeDownHelper(3, 3 * 1000, new RetryForTimeDownHelper.OnRetryCallBack() {
@Override
public void onNext() {
//重试登录
reLogin();
}
@Override
public void onComplete() {
//处理登录失败结果
setFail();
}
});
//登录的实现方法
reLogin();
}
其中回调方法onNext()是进行重试的方法,onComplete()处理重试失败结果
2、在登录失败时调用下重试
public void reLogin() {
LoginHelper.getInstance().login(userId, token, new LoginCallBack(){
@Override
public void onFailed (String errorCode){
if (retryForTimeDownHelper != null) {
RetryForTimeDownHelper.FailBean failBean = new RetryForTimeDownHelper.FailBean();
failBean.setErrorCodeStr(errorCode);
retryForTimeDownHelper.retry(failBean);
}
}
@Override
public void onSuccess () {
retryForTimeDownHelper = null;
}
}
}