熟悉rxjava的同学肯定对操作符不会陌生,比如我们使用map操作符处理数据,使用zip操作符合并多个请求,这里演示下zip操作符的对error情况的处理。
比如说我们同时请求了两个接口,在两个接口都响应的情况下才会展示数据,这里我们使用zip操作符来实现。
zip操作符合并请求
我们先模拟两个请求的响应:responseOneObservable和responseTwoObservable,这里responseTwoObservable我们模拟了错误的场景——onError
.
Observable<ZipOneBean> responseOneObservable = Observable.create(new ObservableOnSubscribe<ZipOneBean>() {
@Override
public void subscribe(ObservableEmitter<ZipOneBean> emitter) {
ZipOneBean zipOneBean = new ZipOneBean();
zipOneBean.setName("zipOneBean");
emitter.onNext(zipOneBean);
emitter.onComplete();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Observable<ZipTwoBean> responseTwoObservable = Observable.create(new ObservableOnSubscribe<ZipTwoBean>() {
@Override
public void subscribe(ObservableEmitter<ZipTwoBean> emitter) {
ZipTwoBean zipTwoBean = new ZipTwoBean();
zipTwoBean.setName("zipTwoBean");
emitter.onError(new RuntimeException("阿西吧"));
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
接着使用zip操作符进行合并:
Observable.zip(responseOneObservable, responseTwoObservable, new BiFunction<ZipOneBean, ZipTwoBean, ArrayList<ZipBaseData>>() {
@Override
public ArrayList<ZipBaseData> apply(ZipOneBean zipOneBean, ZipTwoBean zipTwoBean) {
ArrayList<ZipBaseData> testDataList = new ArrayList();
// Add test data from response responseOne & responseTwo
testDataList.add(zipOneBean);
testDataList.add(zipTwoBean);
return testDataList;
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ArrayList<ZipBaseData>>() {
@Override
public void onSubscribe(Disposable d) {
Log.e(TAG, "onSubscribe");
}
@Override
public void onNext(ArrayList<ZipBaseData> zipBaseData) {
Log.e(TAG, "onNext zipBaseData:" + zipBaseData);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError e:" + e.getMessage());
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete");
}
});
运行代码,输出结果如下:最终走的是onError方法,
E/ZipErrorHandle: onSubscribe
E/ZipErrorHandle: onError e:阿西吧
onErrorReturn
这种做法肯定不能满足需求,如果两个接口中的一个挂了,导致另外一个正常接口的数据也不展示,这个明显不合逻辑。所以我们需要在请求响应错误的时候做下处理,返回默认数据或者特定的数据,标识这个请求失败,不影响别的接口的数据展示。代码如下:
Observable<ZipOneBean> responseOneObservable = Observable.create(new ObservableOnSubscribe<ZipOneBean>() {
@Override
public void subscribe(ObservableEmitter<ZipOneBean> emitter) {
ZipOneBean zipOneBean = new ZipOneBean();
zipOneBean.setName("zipOneBean");
emitter.onNext(zipOneBean);
emitter.onComplete();
}
}).onErrorReturn(new Function<Throwable, ZipOneBean>() {
@Override
public ZipOneBean apply(Throwable throwable) {
ZipOneBean zipOneBean = new ZipOneBean();
zipOneBean.setName("大哥我错了,我是zipOneBean");
return zipOneBean;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Observable<ZipTwoBean> responseTwoObservable = Observable.create(new ObservableOnSubscribe<ZipTwoBean>() {
@Override
public void subscribe(ObservableEmitter<ZipTwoBean> emitter) {
ZipTwoBean zipTwoBean = new ZipTwoBean();
zipTwoBean.setName("zipTwoBean");
emitter.onError(new RuntimeException("阿西吧"));
}
}).onErrorReturn(new Function<Throwable, ZipTwoBean>() {
@Override
public ZipTwoBean apply(Throwable throwable) {
ZipTwoBean zipTwoBean = new ZipTwoBean();
zipTwoBean.setName("大哥我错了,我是zipTwoBean");
return zipTwoBean;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
接下来我们再运行代码,输出如下:
E/ZipErrorHandle: onSubscribe
E/ZipErrorHandle: onNext zipBaseData:[ZipBaseData{name='zipOneBean'}, ZipBaseData{name='大哥我错了,我是zipTwoBean'}]
E/ZipErrorHandle: onComplete
可以看出即使一个请求出错,也是走了成功的回调的。
参考
https://stackoverflow.com/questions/41342844/rxjava-how-to-handle-error-with-zip-operator
网友评论