I am using RxJava and RxAndroid with Retrofit2.
Observable<ResponseOne> responseOneObservable = getRetrofitClient().getDataOne()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Observable<ResponseTwo> responseTwoObservable = getRetrofitClient().getDataTwo()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Using zip operator as below on above two Observer.
Observable<ArrayList<TestData>> testDataObservable = Observable.zip(responseOneObservable, responseTwoObservable, new Func2<ResponseOne, ResponseTwo, ArrayList<TestData>>() {
@Override
public ArrayList<TestData> call(ResponseOne responseOne, ResponseTwo responseTwo) {
ArrayList<TestData> testDataList = new ArrayList();
// Add test data from response responseOne & responseTwo
return testDataList;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ArrayList<TestData>>() {
@Override
public void onNext(ArrayList<TestData> testDataList) {
}
@Override
public void onCompleted() {
Log.d(TAG, "onCompleted" );
}
@Override
public void onError(Throwable t) {
Log.d(TAG, "onError Throwable: " + t.toString() );
}
});
If there is any error occurs during retrofit http call in responseOneObservable
and responseTwoObservable
then it will directly call onError
method of subscriber of testDataObservable
.
I want to continue in zip operator's call
method even if anyone of two observable got success response.
How to handle error response using zip
operator ?
You should use
onErrorResumeNext
over the single zipped observables to instruct them emitting a default item in case of error.Refer to Error-Handling-Operators
You can use
onErrorResumeNext
to return some Observable oronErrorReturn
to return some default value tozip
, like:See onError handling for more info.
You can return default response from either of two observables with
onErrorResumeNext
operator.Also see Error handling in RxJava - Dan Lew