前言
终于到了网络请求模块了。因为以前一直使用的都是Okhttp,所以这次使用Retrofit的话花了两天时间去了解。总的来说Retrofit就是初始化搭建麻烦,挺多东西要注意的。不过看到各界大佬一致好评的请求框架,苦点就苦点。
走过路过点歌Start O(∩_∩)O
这篇文章叫你如何搭建MVP+Retrofit+Rxjava,看完这篇文章你能学会:
- Retrofit的基本使用
- Retrofit + Rxjava + Mvp整合 --------------------------------事不宜迟,上分割线------------------------------------------------ 这一片文章简单到不行,我都一度怀疑有没有必要写。
目录: Retrofit使用思路
- 初始化环境
- 定义API
- 实例化API
- 处理响应定义API
Retrofit 如何结合Rxjava Retrofit+Rxjava如何结合Mvp
Retrofit使用思路:
- 跟Okhttp一样,要先先实例化嘛,做一些初始化环境
- 定义API接口类(Retrofit是通过注解实现的)
- 实例化API,调用接口就能发起请求
- 对请求结果做处理
整体的思路其实跟OKhttp还是很相似的。 初始化环境:
/** 静态模式* */public class RetrofitUtil { /* * BaseUrl * */ private final static String BASE_URL = "http://v.juhe.cn/"; private static Retrofit mRetrofit; private static RetrofitUtil mRetrofitUtil; static { mRetrofitUtil = new RetrofitUtil(); initRetrofit(); } private RetrofitUtil () { } public static RetrofitUtil newInstance() { return mRetrofitUtil; } /* * 初始化网络设置 * */ private static void initRetrofit () { // 设置okhttp OkHttpClient.Builder builder = new OkHttpClient.Builder() .connectTimeout(6000, TimeUnit.SECONDS)//设置超时时间 .retryOnConnectionFailure(true); //添加拦截 HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(); builder.addInterceptor(logInterceptor); OkHttpClient client = builder.build(); //设置retrofit mRetrofit = new Retrofit .Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } /* * 创建api * */ publicT createApi (Class cla) { return mRetrofit.create(cla); }}复制代码
用了单例模式,这里暂时还没做很多通用的一些处理。完成这一步之后等于Retrofit的环境已经创建好了。这个时候就要开始定义具体的请求,通过这个retrofit对象来发起。接着看: 这里要注意的是:BASE_URL可是有要求的,不能随便写;例如后面的路径不能追加在这里。
忘记贴依赖包了:按需添加
// 网络请求 Retrofit + Rxjava implementation 'com.squareup.retrofit2:retrofit:2.3.0'//导入retrofit implementation 'com.google.code.gson:gson:2.6.2'//Gson 库 implementation 'com.squareup.okhttp3:okhttp:3.1.2' implementation 'com.squareup.okhttp3:logging-interceptor:3.1.2' implementation 'com.orhanobut:logger:2.1.0' // 打印日志 //下面两个是RxJava 和 RxAndroid implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.x.y' implementation 'com.squareup.retrofit2:converter-gson:2.3.0'//转换器,请求结果转换成Model implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'//配合Rxjava复制代码
定义API接口:
public interface Api { //http://v.juhe.cn/toutiao/index?type=top&key=APPKEY @GET("/toutiao/index") ObservablegetNews (@Query("type")String type, @Query("key")String apiKey);}复制代码
这些注解的用法使得代码很简洁,只需要多看看官网/教程就知道怎么用了。
实例化API
public class TestModel implements ITest { private static final String TAG = "bigname"; @Override public Observabletest() { RetrofitUtil retrofitUtil = RetrofitUtil.newInstance(); Api api = retrofitUtil.createApi(Api.class); return api.getNews("top", "dc4fed8fb5fb46ac78e51d66306e6762"); }}复制代码
到这里,应用就能够发起请求了。
处理请求响应
public class MainPresenter extends MainContract.MainPresenter { private static final String TAG = "bigname"; @Override public void login(String name, String password) { TestModel testModel = new TestModel(); ObservablenewsDomainObservable = testModel.test(); newsDomainObservable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisposableObserver () { @Override public void onNext(NewsDomain newsDomain) { Log.d(TAG, "onNext: " + newsDomain.toString()); mView.showTips(newsDomain.getReason()); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: " + e); } @Override public void onComplete() { Log.d(TAG, "onComplete: "); } }); }}复制代码
就是这么简单。 看看效果:
如果出现报错:java.net.UnknownServiceException 这是因为API28开始网络请求更加严格引起的,解决方法: 1.在res目录下新建xml资源,然后创建network_security_config.xml文件, 内容:
复制代码
2.在清单文件中:
这样就解决了。
------------------------------------人工分割-------------------------------------------
Retrofit + Rxjava怎么实现?
这个我刚开始以为还多麻烦呢。 Retrofit嵌入Rxjava只需简单几步:
- 导入依赖包rxjava(上面贴的已经在内)
- retrofit初始化增加对rxjava的支持
- 请求响应从Call变成Obserable
依赖就不贴图了, retrofit对rxjava支持:
mRetrofit = new Retrofit .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build();复制代码
要注意的是Rxjava2CallAdapterFactroy()在依赖版本方面需要注意,要对应的。 请求响应从Call变成Obserable
@GET("/toutiao/index") ObservablegetNews (@Query("type")String type, @Query("key")String apiKey);复制代码
如果不适用Rxjava,则返回Call对象,这种方式一样能实现功能。
-------------------------------------------人工分割线---------------------------------------------------
Mvp如何结合进来:
简单到真的没必要打字的感觉。传统的方式是构造ICallBack传递给Model层,然后利用接口回调的方式,现在这种是Model层会返回Obserable观察者对象,Presenter层拿到这个Obserable对象之后把他当成CallBack对象来使用就行了。
ObservablenewsDomainObservable = testModel.test(); newsDomainObservable.subscribeOn(Schedulers.io())...复制代码
The end...