本文介绍了使用 RxJava 调用四个 api 的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 RxJava 的新手
I am new to RxJava
Restapi.class
/************/
@GET("app/dashboard")
Observable<CategoryHomeModel[]> getCategories(@HeaderMap Map<String, String> headers);
@GET("app/wallet/balance")
Observable<WalletBalance> getWalletBalance(@HeaderMap Map<String, String> headers);
@GET("/app/swap/myrateswaps")
Observable<SwapSettings> rateMySwap(@HeaderMap Map<String, String> headers);
@GET("/app/getsettings")
Observable<Settings> getSettings(@HeaderMap Map<String, String> headers);
/************/
我有四个可观察对象
Observable<CategoryHomeModel[]> categoriesObservable = retrofit
.create(Restapi.class)
.getCategories(prepareHeaders())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<WalletBalance> walletObservable = retrofit
.create(Restapi.class)
.getWalletBalance(prepareHeaders())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<Settings> settingsObservable = retrofit
.create(Restapi.class)
.getSettings(prepareHeaders())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<SwapSettings> ratingsObservable = retrofit
.create(Restapi.class)
.rateMySwap(prepareHeaders())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
如何将4个observable合二为一,在一个地方得到响应,同时处理所有observable的响应
How to combine all four observables into one and get the response in one place and handle all the response of all the observables together
我发现这可以通过合并来完成,如果是这样,如何使用合并?
I found out this can be done from Merge, If so how to use the merge ?
推荐答案
使用这种方式
List<Observable<?>> observables = Lists.newArrayList(categoriesObservable, walletObservable, settingsObservable, ratingsObservable);
Observable.merge(observables).toList().subscribe(new Action1<List<Object>>() {
@Override
public void call(List<Object> objects) {
// success
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
// error
}
});
或者你可以使用 mergeDelayError
代替
or you can using mergeDelayError
instead
希望能帮到你
这篇关于使用 RxJava 调用四个 api 的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!