Retrofit & Rxjava3 & RxAndroid

Retrofit & Rxjava3

Reference library

  • implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'The source code mainly analyzed in this article
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0' // rxJava的适配器

implementation "io.reactivex.rxjava3:rxjava:3.1.1"
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'

code example

  • First define an Interface, and the returned object is Observable<T>or Flowable<T>object
  • RxJava3CallAdapterFactoryUsed to configure callAdapter, which RxJava3CallAdapterFactory#create()makes network requests asynchronously by default.
interface GitHubService {
    
    
    @GET("users/{user}/repos")
    Observable<List<Repo>> listRepos(@Path("user") String user);
}
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://api.github.com/") // 配置URL
         // 配置网络请求的adapter,将retrofit2.Call<T>转换为Observable<T>对象
        .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create()) // 配置解析网络返回数据的apdater,解析json
        .build();

GitHubService service = retrofit.create(GitHubService.class);
Observable<List<Repo>> observable = service.listRepos("octocat"); // observable对象类型retrofit2.adapter.rxjava3.CallEnqueueObservable
observable.subscribeOn(Schedulers.io()) // 配置网络请求的IO方式
        .observeOn(AndroidSchedulers.mainThread()) // 在主线程处理收到的数据
        .subscribe(new Observer<List<Repo>>() {
    
     // subscribe函数调用之后才真正的发起网络请求
            @Override
            public void onSubscribe(@NonNull Disposable d) {
    
    
                Log.i(TAG, "onSubscribe");
            }

            @Override
            public void onNext(@NonNull List<Repo> repos) {
    
    
                Log.i(TAG, "onNext");
            }

            @Override
            public void onError(@NonNull Throwable e) {
    
    
                Log.i(TAG, "onError");

            }

            @Override
            public void onComplete() {
    
    
                Log.i(TAG, "onComplete");

            }
        });

RxJava3CallAdapterFactory

  • Generate CallAdapter for making network requests. Convert retrofit2.Call<T>to Observable<T>object.
  • The constructor can configure the scheduler and select whether it is an asynchronous request. RxJava3CallAdapterFactory#createAsynchronous requests are configured by default.
private RxJava3CallAdapterFactory(@Nullable Scheduler scheduler, boolean isAsync) {
  this.scheduler = scheduler;
  this.isAsync = isAsync;
}
  • RxJava3CallAdapterFactory#getThe object will be constructed RxJava3CallAdapterand returned to Retrofit when the network request is made.

RxJava3CallAdapter

  • RxJava3CallAdapter#adapt(Call<R> call)Convert standard retrofit2.Call<T>to Rxjava objects such as Observable<T>, Flowable<T>, Maybe<T>, Single<T>etc.
  • ObservableThe default object type generated by Retrofit is retrofit2.adapter.rxjava3.CallEnqueueObservableor CallExecuteObservable. Reference RxJava3CallAdapter#adapt(Call<R> call).
  • The RxJava3CallAdapter constructor determines the returned object type through several Boolean variables. This also represents com.squareup.retrofit2:adapter-rxjava3the supported Rxjava object types.
retrofit2.adapter.rxjava3.RxJava3CallAdapter#RxJava3CallAdapter
  private final @Nullable Scheduler scheduler; // 被观察者的线程调度器,RxJava3CallAdapterFactory可以配置
  private final boolean isAsync; // 网络请求是同步还是异步
  private final boolean isResult;
  private final boolean isBody; // onNext中参数是response.body()的返回值
  private final boolean isFlowable; // Flowable类型
  private final boolean isSingle; //Single类型
  private final boolean isMaybe; // Maybe 类型
  private final boolean isCompletable;
  • CallEnqueueObservableAnd CallExecuteObservableactually initiated a network request, called retrofit2.Call#enqueueor retrofit2.Call#executemethod.

Guess you like

Origin blog.csdn.net/followYouself/article/details/120570859