封装Retrofit工具类
来源:互联网 发布:阿里云域名testpage 编辑:程序博客网 时间:2024/06/15 09:49
这个是Retrofit和Rxjava结合的,首先导入需要的依赖:
观察者Observer,这两种依赖必须同时都用compile 'io.reactivex:rxjava:1.0.14'compile 'io.reactivex:rxandroid:1.0.1'Retrofit的依赖compile 'com.squareup.retrofit2:retrofit:2.0.2' compile 'com.squareup.okhttp3:okhttp:3.1.2'compile 'com.squareup.retrofit2:converter-gson:2.0.2'compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
先说一下怎么使用吧。
OkHttpClient client=new OkHttpClient.Builder().build(); RetrofitManager.getInstance(Constant.BASE_URL,client).setCreate(UserUtil.class).getRecycle(21).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(observer);
工具类:
public class RetrofitManager { private Retrofit mRetrofit; private String baseUrl; OkHttpClient client;//有关拦截器 private static RetrofitManager mRetrofitManager; private RetrofitManager(){} private RetrofitManager(String baseUrl,OkHttpClient client){ this.baseUrl=baseUrl; this.client=client;//有关拦截器 initRetrofit(); } public static synchronized RetrofitManager getInstance(String baseUrl,OkHttpClient client){//有关拦截器 if (mRetrofitManager == null){ mRetrofitManager = new RetrofitManager(baseUrl,client); } return mRetrofitManager; } private void initRetrofit() { mRetrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client)//有关拦截器 .build(); } public <T> T setCreate(Class<T> reqServer){ return mRetrofit.create(reqServer); }}根据自己的需求选择,不用拦截器的话,就把有关拦截器的代码删除。
阅读全文
0 0
- 封装Retrofit工具类
- Retrofit工具类(Retrofit二次封装)
- Retrofit工具类的封装
- Retrofit单例封装工具类
- Retrofit+Rxjava封装类
- Retrofit -> 封装
- Retrofit封装
- Retrofit封装
- Retrofit封装
- 封装Retrofit
- Retrofit+Rxjava请求工具类
- Retrofit的使用封装
- flux-retrofit-okhttp封装
- Retrofit项目封装使用
- Retrofit框架基本封装
- Retrofit+Rxjava的封装
- Retrofit+RxJava 完美封装
- Retrofit简单封装
- I/O exception (java.net.SocketException) caught when processing request: Connect
- 大数据1-hadoop、zookeeper、hbase、spark集群环境搭建
- 干货分享之电脑装机
- Qcom音频调试杂记-DRC
- Coursea吴恩达《结构化机器学习项目》课程笔记(1)机器学习策略上篇
- 封装Retrofit工具类
- makefile的隐式规则
- Linux下设置和查看环境变量
- 通过relocate 创建pdb, 即将pdb从一个CDB MOVE 到另一个CDB
- 02-单元测试框架
- 关于Hibernate5.2之后createCriteria(),@deprecated(过时的)
- SQL SERVER 表分区
- 放弃使用Android Studio,搭建Android命令行开发环境
- 给Lua添加新的C模块(.so文件) 例子