One, ReactiveX
If you want to learn RxJava, you have to mention its origin, ReactiveX, which is an API that focuses on asynchronous programming and controlling the flow of observable data (or events). It combines the observer pattern, iterator pattern and the excellent ideas of functional programming. ReactiveX is the abbreviation of Reactive Extensions, generally abbreviated as Rx, originally an extension of LINQ, developed by a team led by Microsoft architect Erik Meijer;
Second, the development of Rx
Rx has become so popular in recent years that many language versions have been developed, such as RxJava, RxGo, RxJS, RxKotlin, RxPY , Rx.NET, etc.; most of the language libraries of Rx are maintained by the organization ReactiveX, and the community website is reactivex .io .
Three, RxJava
RxJava is a java implementation of Reactive Extensions, which implements an asynchronous programming interface based on the observer pattern.
The official github website of Rxjava 3.x ;
Some changes in Rxjava 3.0: Official Wiki ;
Rxjava 3.x document can be official javadoc found in
1. Combined use of Retrofit + RxJava3
First introduce dependency
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'
Build.gradle under related configuration module
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Note that when Retrofit and RxJava are used in combination, the rxjava adapter (adapter-rxjava3) used in Retrofit must be the same as the RxJava version (io.reactivex.rxjava3:rxjava:3.0.0); in this example, 3.0; about the previous Rerotfit has no Rxjava3.0 adapter problem;
If you want to use rxjava adaptation when creating Retrofit, be careful not to make a mistake. The correct posture is as follows:
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava3CallAdapterFactory.createSynchronous())
// Or
// .addCallAdapterFactory(RxJava3CallAdapterFactory.createWithScheduler(Schedulers.io()))
.baseUrl("")
.build();
2. Combined use of Retrofit + RxJava
If you want to use 1.0, you can add dependencies like this
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'
Correct posture created by Retrofit:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.baidu.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
3. Combined use of Retrofit + RxJava2.0
The use of RxJava2.0 should start with an exception:
abnormal:
Could not locate call adapter for io.reactivex.Observable<okhttp3.ResponseBody>.
compile'com.squareup.retrofit2:adapter-rxjava:2.1.0' RxJava adapter is 1.0
So it’s okay to change to RxJava2.0, because the Android project uses RxJava2.0, then Retrofit should be used with Rxjava, and the corresponding Rxjava2.0 adapter should be configured; and so on. If you want to use Rxjava3.0, then the corresponding Retrofit middle
Two ways to configure the RxJava2.0 adapter used in Retrofit
1. RxJava2.0 adapter provided by a third party
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
2. Starting from Retrofit 2.2.0 version, RxJava2 has an official adapter:
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(new OkHttpClient.Builder().build())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();
return retrofit.create(serviceClass);
Fourth, the specific use cases of Retrofit+Rxjava2
Retrofit+Rxjava2 realizes the function of batch downloading of pictures