我对rxJava非常陌生,我想使用Retrofit将2个api调用按顺序链接在一起。但是不断出现此错误:无法为rx创建调用适配器。方法xx可观察到。以下是我的代码的一部分:
ServiceA
.subscribeOn(rx.schedulers.Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Func1<ResponseA, Observable<ResponseB>>() {
@Override
public rx.Observable<ResponseB> call(ResponseA fooA) {
// code to save data from service A to db
// call service B
return ServiceB;
}
})
.subscribe(new Subscriber<ResponseB>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(ResponseB fooB) {
// code to save data from service B to db
}
});
Retrofit retrofit = new Retrofit.Builder().baseUrl("xxx")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.3'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
答案 0 :(得分:0)
您需要执行类似的操作
val loginRequest = retrofitService.login(params)
loginRequest
.flatMap { response ->
saveLoginInfo(response.data)
return@flatMap retrofitService.getProfile(params) }
.flatMap { response ->
doSomethingWithProfile(response.data)
return@flatMap retrofitService.getItems(params) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe (
{ response ->
doSomethingWithItems(response.data)
doSomethingInMainThread()
},
{ err -> Log.v("TAG", err.localizedMessage) },
{ Log.v("TAG", "Chains Completed") }
)
在link
中查看更多信息