Retrofit2源码解析(三)添加 RxJavaCallAdapterFactory适配器
来源:互联网 发布:良辰好景知几何书包网 编辑:程序博客网 时间:2024/06/10 17:23
1、获取实例时,我们再添加一个适配器
retrofit = new Retrofit.Builder().baseUrl("http://localhost:8080/campus/") .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(JacksonConverterFactory.create()) .build();
老规则,这里只是添加了一个适配器,所以上面两篇的返回类型还是支持的。
看下RxJavaCallAdapterFactory的源码,代码比较多,但我们只需要看 CallAdapter
retrofit2.adapter.rxjava.RxJavaCallAdapterFactory.java//支持返回类型为rx.Observable、rx.Single、rx.Completable//根据不同的类型生成对应的CallAdapter的实现类 @Override public CallAdapter<?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) { Class<?> rawType = getRawType(returnType); String canonicalName = rawType.getCanonicalName(); boolean isSingle = "rx.Single".equals(canonicalName); boolean isCompletable = "rx.Completable".equals(canonicalName); if (rawType != Observable.class && !isSingle && !isCompletable) { return null; } if (!isCompletable && !(returnType instanceof ParameterizedType)) { String name = isSingle ? "Single" : "Observable"; throw new IllegalStateException(name + " return type must be parameterized" + " as " + name + "<Foo> or " + name + "<? extends Foo>"); } if (isCompletable) { return CompletableHelper.createCallAdapter(scheduler); } CallAdapter<Observable<?>> callAdapter = getCallAdapter(returnType, scheduler); if (isSingle) { return SingleHelper.makeSingle(callAdapter); } return callAdapter; }
所以再在可以将接口改成如下
public interface HttpLoginRx { @POST("account/login") Observable<HashMap<String, Object>> login(@Body Account account); }
也是ok的。
0 0
- Retrofit2源码解析(三)添加 RxJavaCallAdapterFactory适配器
- Retrofit源码解析:RxJavaCallAdapterFactory
- Retrofit源码解析---执行请求(RxJavaCallAdapterFactory)
- Retrofit源码解读(三)--RxJavaCallAdapterFactory讲解
- Retrofit2源码解析(二)添加 JacksonConverterFactory转化器
- Retrofit2源码解析(一)
- Retrofit2源码解析(一)
- Retrofit2.0源码解析(简版)
- Retrofit2源码解析
- Retrofit2 源码解析
- Retrofit2.0源码解析
- Retrofit2 源码解析
- Retrofit2 源码解析
- Retrofit2 源码解析
- Retrofit2 源码解析
- Retrofit2 源码解析
- Retrofit2 源码解析
- Retrofit2.1源码解析
- Fragment与RecyclerView内存溢出
- centos 安装CutyCapt
- MySQL 计算时间差并求和与填充默认值(IFNULL)
- Linux系统备份与恢复
- TestNG系列-第二章 注解介绍
- Retrofit2源码解析(三)添加 RxJavaCallAdapterFactory适配器
- Python写的AES文件加密模块
- stuts2接收json返回json案例
- Oracle 删除一个用户下的所有表、触发器、约束
- RxJava(RxAndroid)(二)(笔记)
- sea.js的模块化开发
- TestNG系列-第三章 testNG运行方法
- 关于oracle数据库维护数据的完整性--约束
- 第十一周--项目2 - 用二叉树求解代数表达式