Retrofit2使用CallAdapterFactory的错误处理
来源:互联网 发布:linux用ubuntu 编辑:程序博客网 时间:2024/06/11 23:27
概述
在使用Retrofit1的时候有一个类叫ErrorHandler
,可以方便的定义自己的异常处理,但是在Retrofit2
中是没有这个类的,详情见讨论:How do we create ErrorHandler in retrofit 2.0? #1102
总结
在相关讨论之下发现了一种好的解决方案,详情见: Retrofit 2 and Rx Java call adapter error handling,主要通过CallAdapter.Factory
来实现,核心代码
- 定义CallAdapter
public class RxErrorHandlingCallAdapterFactory extends CallAdapter.Factory { private final RxJavaCallAdapterFactory original; private RxErrorHandlingCallAdapterFactory() { original = RxJavaCallAdapterFactory.create(); } public static CallAdapter.Factory create() { return new RxErrorHandlingCallAdapterFactory(); } @Override public CallAdapter<?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { return new RxCallAdapterWrapper(retrofit, original.get(returnType, annotations, retrofit)); } private static class RxCallAdapterWrapper implements CallAdapter<Observable<?>> { private final Retrofit retrofit; private final CallAdapter<?> wrapped; public RxCallAdapterWrapper(Retrofit retrofit, CallAdapter<?> wrapped) { this.retrofit = retrofit; this.wrapped = wrapped; } @Override public Type responseType() { return wrapped.responseType(); } @SuppressWarnings("unchecked") @Override public <R> Observable<?> adapt(Call<R> call) { return ((Observable) wrapped.adapt(call)).onErrorResumeNext(new Func1<Throwable, Observable>() { @Override public Observable call(Throwable throwable) { return Observable.error(asRetrofitException(throwable)); } }); } private RetrofitException asRetrofitException(Throwable throwable) { // We had non-200 http error if (throwable instanceof HttpException) { // A network error happened } if (throwable instanceof IOException) { } // We don't know what happened. We need to simply convert to an unknown error return RetrofitException.unexpectedError(throwable); } }}
- 定义Exception
public class RetrofitException extends RuntimeException { public static RetrofitException httpError(String url, Response response, Retrofit retrofit) { String message = response.code() + " " + response.message(); return new RetrofitException(message, url, response, Kind.HTTP, null, retrofit); } public static RetrofitException networkError(IOException exception) { return new RetrofitException(exception.getMessage(), null, null, Kind.NETWORK, exception, null); } public static RetrofitException unexpectedError(Throwable exception) { return new RetrofitException(exception.getMessage(), null, null, Kind.UNEXPECTED, exception, null); }}
- 设置到Builder
new Retrofit.Builder() .baseUrl("your base url") .addConverterFactory(GsonConverterFactory.create(new Gson())) .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create()) .build();
- 错误处理
public void onError(Throwable throwable) { if (throwable instanceof HttpException) { // We had non-2XX http error } if (throwable instanceof IOException) { // A network or conversion error happened } // We don't know what happened. We need to simply convert to an unknown error // ... }
相关代码整理: retrofit2
0 0
- Retrofit2使用CallAdapterFactory的错误处理
- Retrofit2.0的使用
- Retrofit2.0 的使用
- Retrofit2.0的使用
- Retrofit2的使用
- Retrofit2.0的使用
- Retrofit2 简单的使用
- Android Retrofit2的使用
- Retrofit2 的简单使用
- Retrofit2的使用
- Retrofit2的简单使用
- Retrofit2的简单使用
- Retrofit2的使用
- Retrofit2.0的Converter使用
- Retrofit2 的使用以及总结
- Retrofit2.0 的初步使用
- retrofit2+rxjava的结合使用
- retrofit2.0的简单使用
- Http协议备忘录
- 目前为止,在图像缩放项目遇到的一些坑
- 【HOJ1867】经理的烦恼-区间求和+判素数
- Java 聊天室
- iOS面试题,看看你究竟知道多少(二)
- Retrofit2使用CallAdapterFactory的错误处理
- day07_xml学习
- Gradle 详解
- Java面向对象二
- android Cannot execute task: the task has already been executed (a task can be executed only once)
- 2016 多校联训 第三场Teacher Bo hdoj5762
- webpack-dev-server+vue构建开发环境(一)
- 单例模式--Singleton模式的几种解法分析
- ViewPager无限循环滑动无卡顿,详解OnPageChangeListener