我有以下代码,我将改型与RX结合使用来进行多个调用,为简便起见,我仅添加了三个调用:
public void getDashboardData(final String userKey, final String sign) {
final DashboardDataApi service = RetrofitInstance.getRetrofitInstance().create(DashboardDataApi.class);
Observable<ResponseBody> aGetApi = service.aGetApi(userKey, sign);
final Observable<ResponseBody> dGet = service.dGet(userKey, sign);
final Observable<ResponseBody> bOnGet = service.bonGet(userKey, sign);
final List<ResponseBody> result = new ArrayList<>();
Disposable disposable = aGetApi
.subscribeOn(Schedulers.io())
.flatMap(new Function<ResponseBody, ObservableSource<ResponseBody>>() {
@Override
public ObservableSource<ResponseBody> apply(ResponseBody response1) {
result.add(response1);
return dGet;
}
})
.flatMap(new Function<ResponseBody, ObservableSource<ResponseBody>>() {
@Override
public ObservableSource<ResponseBody> apply(ResponseBody response1) {
result.add(response1);
return bOnGet;
}
}).map(new Function<ResponseBody, ResponseBody>() {
@Override
public ResponseBody apply(ResponseBody response3) {
result.add(response3);
return response3;
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<ResponseBody>() {
@Override
public void onNext(ResponseBody responseBody) {
try {
String jsonData = responseBody.string();
Log.d("RX", "RESP: " + jsonData);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
Log.d("RX", "Error");
e.printStackTrace();
}
@Override
public void onComplete() {
Log.d("RX", "RX Flat Map chaining complete");
}
});
}
现在我被困在以下地方:
1)如果一个Api失败,我必须等待并重试,直到获得结果。 2)一切准备就绪后,我必须填充结果对象。
我对RX来说还很陌生,所以现在还不多。