java RxJava2和Retrofit 2.2.0兼容工厂,它包装{@link RxJava2CallAdapterFactory}并负责错误转换。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java RxJava2和Retrofit 2.2.0兼容工厂,它包装{@link RxJava2CallAdapterFactory}并负责错误转换。相关的知识,希望对你有一定的参考价值。

/**
 * RxJava2 and Retrofit 2.2.0 compatible factory,
 * which wraps the {@link RxJava2CallAdapterFactory} and takes care of the error conversion.
 *
 * Based on: https://github.com/square/retrofit/issues/1102#issuecomment-241250796
 */
public class RxErrorHandlingCallAdapterFactory extends CallAdapter.Factory {
    private final RxJava2CallAdapterFactory mOriginalCallAdapterFactory;

    private RxErrorHandlingCallAdapterFactory() {
        mOriginalCallAdapterFactory = RxJava2CallAdapterFactory.create();
    }

    public static CallAdapter.Factory create() {
        return new RxErrorHandlingCallAdapterFactory();
    }

    @Override
    public CallAdapter<?, ?> get(final Type returnType, final Annotation[] annotations, final Retrofit retrofit) {
        return new RxCallAdapterWrapper<>(retrofit, mOriginalCallAdapterFactory.get(returnType, annotations, retrofit));
    }

    private static class RxCallAdapterWrapper<R> implements CallAdapter<R, Observable<R>> {
        private final Retrofit mRetrofit;
        private final CallAdapter<R, ?> mWrappedCallAdapter;

        public RxCallAdapterWrapper(final Retrofit retrofit, final CallAdapter<R, ?> wrapped) {
            mRetrofit = retrofit;
            mWrappedCallAdapter = wrapped;
        }

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

        @SuppressWarnings("unchecked")
        @Override
        public Observable<R> adapt(final Call<R> call) {
            return ((Observable) mWrappedCallAdapter.adapt(call)).onErrorResumeNext(new Function<Throwable, ObservableSource>() {
                @Override
                public Observable apply(final Throwable throwable) {
                    return Observable.error(asRetrofitException(throwable));
                }
            });
        }

        private RetrofitException asRetrofitException(final Throwable throwable) {
            // We had non-200 http error
            if (throwable instanceof HttpException) {
                final HttpException httpException = (HttpException) throwable;
                final Response response = httpException.response();

                return RetrofitException.httpError(response.raw().request().url().toString(), response, mRetrofit);
            }
            // A network error happened
            if (throwable instanceof IOException) {
                return RetrofitException.networkError((IOException) throwable);
            }

            // We don't know what happened. We need to simply convert to an unknown error

            return RetrofitException.unexpectedError(throwable);
        }
    }
}

以上是关于java RxJava2和Retrofit 2.2.0兼容工厂,它包装{@link RxJava2CallAdapterFactory}并负责错误转换。的主要内容,如果未能解决你的问题,请参考以下文章

retrofit2+MVP+rxjava2+rxlifecycle2 为啥无法解决内存泄露

Android实战——RxJava2+Retrofit+RxBinding解锁各种新姿势

如何使用Retrofit2,RxJava2,Gson TypeAdapterFActory正确映射Gson?

使用 RxJava2 和 Retrofit2 时如何访问响应头?

retrofit 2 支持rxjava2了吗

浅谈Retrofit2+Rxjava2