一,ReactiveX
要學習RxJava,那么不得不提他的由來ReactiveX,ReactiveX 是一個專注于異步編程與控制可觀察資料(或者事件)流的API,它組合了觀察者模式,迭代器模式和函式式編程的優秀思想,ReactiveX是Reactive Extensions的縮寫,一般簡寫為Rx,最初是LINQ的一個擴展,由微軟的架構師Erik Meijer領導的團隊開發;
二,Rx的發展
Rx這幾年非常流行,以至于開發出多種語言版本,例如RxJava 、 RxGo 、RxJS、RxKotlin、RxPY、Rx.NET等等;Rx的大部分語言庫由ReactiveX這個組織負責維護,社區網站是 reactivex.io,
三,RxJava
RxJava是回應式編程(Reactive Extensions)的java實作,它基于觀察者模式的實作了異步編程介面,
Rxjava 3.x 的github官網;
Rxjava 3.0的一些改變:官方Wiki;
Rxjava 3.x 檔案可以在官方javadoc中找到
RxAndroid 3.0 GitHub官網
1,Retrofit + RxJava3組合使用
首先要引入依賴
implementation "io.reactivex.rxjava3:rxjava:3.0.0"
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'com.squareup.retrofit2:retrofit:2.7.0'
implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
相關配置module下的build.gradle
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
注意Retrofit和RxJava組合使用時,Retrofit中使用的rxjava配接器(adapter-rxjava3)要和RxJava版本(io.reactivex.rxjava3:rxjava:3.0.0)一致;如本例都是使用的時3.0;關于先前Rerotfit沒有Rxjava3.0配接器問題;
創建Retrofit時如果要使用rxjava適配,注意不要寫錯,正確姿勢如下代碼:
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava3CallAdapterFactory.createSynchronous())
// Or
// .addCallAdapterFactory(RxJava3CallAdapterFactory.createWithScheduler(Schedulers.io()))
.baseUrl("")
.build();
2,Retrofit + RxJava組合使用
如果要使用1.0可以這樣添加依賴
implementation 'io.reactivex:rxjava:1.0.14
implementation 'io.reactivex:rxandroid:1.0.1'
implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
Retrofit創建的正確姿勢:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.baidu.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
3,Retrofit + RxJava2.0組合使用
關于RxJava2.0的使用要從一個例外說起:
例外:
Could not locate call adapter for io.reactivex.Observable<okhttp3.ResponseBody>.
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' RxJava配接器是1.0
所以換成RxJava2.0就可以了,原因Android專案中使用的是RxJava2.0,那么Retrofit要配合Rxjava使用,相應的要配置Rxjava2.0配接器;以此類推如果要使用Rxjava3.0那么相應的Retrofit中的
兩種方式配置Retrofit中使用的RxJava2.0配接器
1,第三方的提供的RxJava2.0配接器
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
2,從Retrofit 2.2.0版開始,RxJava2配接器有一個官方提供的:
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
然后修改Retrofit創建時代碼:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(new OkHttpClient.Builder().build())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();
return retrofit.create(serviceClass);
具體還可以參考
https://stackoverflow.com/questions/43434073/unable-to-create-call-adapter-for-io-reactivex-observable
Retrofit和Rxjava2.0配合使用需要的依賴
implementation 'com.squareup.retrofit2:retrofit:2.2.0'//retrofit
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0' //配合Rxjava 使用的配接器
//下面兩個是RxJava 和RxAndroid
implementation 'io.reactivex.rxjava2:rxjava:2.2.18'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
四,Retrofit+Rxjava2具體使用案例
Retrofit+Rxjava2實作圖片批量下載的功能
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/192763.html
標籤:其他
上一篇:面試:簡述微信小程式的原理
下一篇:賬號分享ios《Lanota》
