My use case is: I have an Android app, I need to call one api, when that data becomes available call the second api. For this I was planning to use RxAndroid, with retries.
I was able to do 1 api call with retries, but I need to chain the 2, and be able to show 2 different errors, one for call 1 not available, and one for call2 not available.
I can have the retry either on the whole chain, but I would prefer to retry each operation individually.
My code is as follow, I need to add a "callApi1()" before the callApi2, and I would prefer, like I said to have a different observable with retries.
Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
subscriber.onNext(callApi2());
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
}
})
.retryWhen(new RetryWithDelay(20, 3000))
.timeout(TIME_OUT_SECONDS, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<String>() {
@Override
public void onNext(String data) {
//show something
}
....
}
You could use the Observable.concat operator eg
Observable<String> obs1 = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
subscriber.onNext(callApi1());
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
}
})
.timeout(TIME_OUT_SECONDS, TimeUnit.SECONDS)
.retryWhen(new RetryWithDelay(20, 3000))
.subscribeOn(Schedulers.io());
Observable<String> obs2 = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
subscriber.onNext(callApi2());
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
}
})
.timeout(TIME_OUT_SECONDS, TimeUnit.SECONDS)
.retryWhen(new RetryWithDelay(20, 3000))
.subscribeOn(Schedulers.io());
Observable<String> chained = Observable.concat(obs1,obs2);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.