[英]Can we use Retrofit, RxJava, RxAndroid together?
我用过:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:retrofit
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
我应该这样使用吗,我读过retrofit 1.x
本身在工作线程上的工作? 我应该同时使用rxJava
和retrofit
还是只对我retrofit
工作?
是的你可以。
您应该在gradle中添加以下依赖项:
dependencies {
...
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.2.1'
...
}
清单中的权限:
<uses-permission android:name="android.permission.INTERNET" />
服务创建者可能是这样的:
public class RxServiceCreator {
private OkHttpClient.Builder httpClient;
private static final int DEFAULT_TIMEOUT = 30; //seconds
private Gson gson;
private RxJavaCallAdapterFactory rxAdapter;
private Retrofit.Builder builder;
private static RxServiceCreator mInstance = null;
private RxServiceCreator() {
httpClient = new OkHttpClient.Builder();
gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
rxAdapter = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io());
builder = new Retrofit.Builder()
.baseUrl("yourbaseurl")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(rxAdapter);
if (BuildConfig.REST_DEBUG) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(logging);
}
httpClient.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
httpClient.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
httpClient.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
}
public static RxServiceCreator getInstance() {
if (mInstance == null) {
mInstance = new RxServiceCreator();
}
return mInstance;
}
public <RESTService> RESTService createService(Class<RESTService> service) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(service);
}
}
您的界面可能是这样的:
public interface UserAPI {
@GET("user/get_by_email")
Observable<Response<UserResponse>> getUserByEmail(@Query("email") String email);
}
最后进行api调用:
UserAPI userApi = RxServiceCreator.getInstance().createService(UserAPI.class);
userApi.getUserByEmail("user@example.com")
.observeOn(AndroidSchedulers.mainThread())
.subscribe(userResponse -> {
if (userResponse.isSuccessful()) {
Log.i(TAG, userResponse.toString());
} else {
Log.i(TAG, "Response Error!!!");
}
},
throwable -> {
Log.e(TAG, throwable.getMessage(), throwable);
});
是的,您可以一起使用,也可以更好。 带有改造的Rxjava
如果您有简单的休息服务电话,并且不需要任何转换,那么仅使用Retrofit就足够了。 您可以在后台线程上运行改造服务调用,并使用主线程上的响应更新UI。 但是问题是您需要注意防止内存泄漏。 您需要确保在销毁活动时销毁所有对象,以防止内存泄漏。
但是对于特定的工作流程,如果您需要进行多个服务调用或过滤或转换响应,那么Retrofit和RxJava2将是正确的选择。 使用CompositeDisposable的RxJava可以很容易地防止内存泄漏。
如果您想构建响应式应用程序,而无需考虑其他服务调用的复杂性,请使用RxJava2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.