Android OKHTTP 网络请求出错重连(结合Retrofit RxJava)--retryWhen命令符

链接:
https://www.jianshu.com/p/508c30aef0c1
网络抖动可能会产生大量的网络断链,所以httpClient自动重试是非常有必要的

需求1:根据异常类型选择是否重试
* 即,当发生的异常 = 网络异常 = IO异常 才选择重试
需求2:实现重试
* 通过返回的Observable发送的事件 = Next事件,从而使得retryWhen()重订阅,最终实现重试功能

需求3:延迟1段时间再重试
* 采用delay操作符 = 延迟一段时间发送,以实现重试间隔设置

需求4:遇到的异常越多,时间越长
* 在delay操作符的等待时间内设置 = 每重试1次,增多延迟重试时间0.5s
Android OKHTTP 网络请求出错重连(结合Retrofit RxJava)--retryWhen命令符_第1张图片

Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(mBuilder.mBaseUrl)
                .client(providerHttpClient())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(FRxJava2CallAdapterFactory.createWithScheduler());
public class FRxJava2CallAdapterFactory extends CallAdapter.Factory {
    private RxJava2CallAdapterFactory wrappedCallAdapterFactory;

    private FRxJava2CallAdapterFactory() {
        wrappedCallAdapterFactory = RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io());
    }

    public static FRxJava2CallAdapterFactory createWithScheduler() {
        return new FRxJava2CallAdapterFactory();
    }

    @Override
    public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
        CallAdapter<?, ?> callAdapter = wrappedCallAdapterFactory.get(returnType, annotations,
                retrofit);
        if (callAdapter == null) {
            return null;
        }
        Class<?> rawType = getRawType(returnType);
        boolean isCompletable = rawType == Completable.class;
        boolean isFlowable = rawType == Flowable.class;
        boolean isSingle = rawType == Single.class;
        boolean isMaybe = rawType == Maybe.class;
        return new FCallAdapter(callAdapter, isCompletable, isFlowable, isMaybe, isSingle);
    }

    private static class FCallAdapter<R> implements CallAdapter<R, Observable> {
        //we might handle other rawtype in the future. but currently only for Observable.
        private final boolean isCompletable;
        private final boolean isFlowable;
        private final boolean isMaybe;
        private final boolean isSingle;
        private CallAdapter wrapped;

        FCallAdapter(CallAdapter callAdapter, boolean isCompletable,
                     boolean isFlowable, boolean isMaybe, boolean isSingle) {
            wrapped = callAdapter;
            this.isCompletable = isCompletable;
            this.isFlowable = isFlowable;
            this.isMaybe = isMaybe;
            this.isSingle = isSingle;
        }

        @Override
        public Type responseType() {
            return wrapped.responseType();
        }

        @Override
        public Observable<?> adapt(Call<R> call) {
            String retryOnFailure = call.request().header("retryOnFailure");
            boolean retry = retryOnFailure != null && Boolean.valueOf(retryOnFailure);
            return ((Observable) wrapped.adapt(call))
//                    .onErrorResumeNext()
//                    .timeout(10 * 1000, TimeUnit.MILLISECONDS)
                    .subscribeOn(Scheduler.ioThread())
                    .observeOn(Scheduler.mainThread())
                    .doAfterNext(new Consumer() {
                        @Override
                        public void accept(Object o) throws Exception {
//                            if (!IOVCloudRepository.getInstance().getIovCloudRepositoryManager
// ().isNetworkAvailable()) {
//                                FFUtils.checkNetworkAccess(null);
//                            }
                        }
                    })
                    .retryWhen(new RetryWhenNetworkException(2))//重试逻辑
                    ;

        }




    }



}

主要的重试逻辑

public class RetryWhenNetworkException implements Function<Observable<? extends Throwable>, Observable<?>> {
    // 可重试次数
    private int maxConnectCount = 2;
    // 当前已重试次数
    private int currentRetryCount = 0;
    // 重试等待时间
    private int waitRetryTime = 0;


    public RetryWhenNetworkException(int maxConnectCount) {
        this.maxConnectCount = maxConnectCount;
    }


    @Override
    public Observable<?> apply(Observable<? extends Throwable> throwableObservable) throws Exception {
        // 参数Observable中的泛型 = 上游操作符抛出的异常,可通过该条件来判断异常的类型
        return throwableObservable.flatMap(new Function<Throwable, ObservableSource<?>>() {
            @Override
            public ObservableSource<?> apply(Throwable throwable) throws Exception {
                // 输出异常信息
                FLog.d("发生异常 = " + throwable.toString());

                /**
                 * 需求1:根据异常类型选择是否重试
                 * 即,当发生的异常 = 网络异常 = IO异常 才选择重试
                 */
                if (throwable instanceof IOException ) {
//                if (throwable instanceof TimeoutException ) {
//                    FLog.d("属于IO异常,需重试");
                    FLog.d("属于网络异常,需重试");

                    /**
                     * 需求2:限制重试次数
                     * 即,当已重试次数 < 设置的重试次数,才选择重试
                     */
                    if (currentRetryCount < maxConnectCount) {

                        // 记录重试次数
                        currentRetryCount++;
                        FLog.d("重试次数 = " + currentRetryCount);

                        /**
                         * 需求2:实现重试
                         * 通过返回的Observable发送的事件 = Next事件,从而使得retryWhen()重订阅,最终实现重试功能
                         *
                         * 需求3:延迟1段时间再重试
                         * 采用delay操作符 = 延迟一段时间发送,以实现重试间隔设置
                         *
                         * 需求4:遇到的异常越多,时间越长
                         * 在delay操作符的等待时间内设置 = 每重试1次,增多延迟重试时间0.5s
                         */
                        // 设置等待时间
                        waitRetryTime = 500 + currentRetryCount * 500;
                        FLog.d("等待时间 =" + waitRetryTime);
                        return Observable.just(1).delay(waitRetryTime, TimeUnit.MILLISECONDS);


                    } else {
                        // 若重试次数已 > 设置重试次数,则不重试
                        // 通过发送error来停止重试(可在观察者的onError()中获取信息)
                        return Observable.error(new Throwable("重试次数已超过设置次数 = " + currentRetryCount + ",即 不再重试;"+throwable));

                    }
                }

                // 若发生的异常不属于I/O异常,则不重试
                // 通过返回的Observable发送的事件 = Error事件 实现(可在观察者的onError()中获取信息)
                else {
                    FLog.e(new Throwable("发生了非网络异常(非I/O异常)"));
                    return Observable.error(throwable);
                }
            }
        });
    }
}

你可能感兴趣的:(Android)