RX Java with Retrofit-2 VS Normal Retrofit-2

Rag*_*han 1 android android-layout rx-java retrofit2

我目前Retrofit2用于API解析。因为我被要求用RxJava+Retrofit为我的新应用程序更改它。我怎样才能做到这一点。什么是使用的好处RxJava沿Retrofit

任何帮助都应该受到高度赞赏。

下面是我用于正常Retrofit解析的代码

Retrofit.Builder builder =new Retrofit.Builder().baseUrl(API_BASE_URL).client(httpClient).addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
Run Code Online (Sandbox Code Playgroud)

laa*_*lto 5

因为我被要求使用 RxJava + Retrofit 为我的新应用程序更改它。我怎样才能做到这一点。

com.squareup.retrofit2:adapter-rxjava2在您的项目中添加为依赖项并在您的Retrofit.Builder:

addCallAdapterFactory(
    RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
Run Code Online (Sandbox Code Playgroud)

然后你可以改变你的改造接口

Call<ReturnType> op(...)
Run Code Online (Sandbox Code Playgroud)

Observable<ReturnType> op(...)
Run Code Online (Sandbox Code Playgroud)

而不是enqueue()ing Call,订阅 observable 来让你的请求飞起来。

使用 RxJava 和 Retrofit 有什么好处

改造服务 API 调用与应用程序中的其他 rxjava 代码很好地集成。如果您没有在应用程序的其他地方使用 rxjava,则没有什么好处。