9. Rxjava2 : error处理

2019-02-13  本文已影响69人  0青衣小褂0

1. RxJava2 : 什么是观察者模式
2. RxJava2 : 创建操作符(无关时间)
3. Rxjava2 : 创建操作符(有关时间)
4. Rxjava2 : 变换操作符
5. Rxjava2 : 判断操作符
6. Rxjava2 : 筛选操作符
7. Rxjava2 : 合并操作符
8. Rxjava2 : do操作符
9. Rxjava2 : error处理
10. Rxjava2 : 重试
11. Rxjava2 : 线程切换

api use
onErrorReturn {{onErrorReturn}}
onErrorResumeNext {{onErrorResumeNext}}
onExpectionResumeNext {{onExpectionResumeNext}

onErrorReturn

Observable.create((ObservableOnSubscribe<Integer>) e -> {
            e.onNext(1);
            e.onError(new NullPointerException());
            e.onNext(2);
        }).onErrorReturn(throwable -> 666)
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "integer:" + integer);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete");
                    }
                });

log

02-03 15:18:02.628 1869-1869/... D/aaabccc: integer:1
02-03 15:18:02.628 1869-1869/... D/aaabccc: integer:666
02-03 15:18:02.638 1869-1869/... D/aaabccc: onComplete

onErrorResumeNext

Observable.create((ObservableOnSubscribe<Integer>) e -> {
            e.onNext(1);
            e.onError(new Error());
            e.onNext(2);
        }).onErrorResumeNext(throwable -> {
            return Observable.just(666,777);
        })
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "integer:" + integer);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete");
                    }
                });
    }

log

02-03 15:47:35.608 5890-5890/... D/aaabccc: integer:1
02-03 15:47:35.608 5890-5890/... D/aaabccc: integer:666
02-03 15:47:35.608 5890-5890/... D/aaabccc: integer:777
02-03 15:47:35.608 5890-5890/... D/aaabccc: onComplete

onExpectionResumeNext

// throwable: Exception
Observable.create((ObservableOnSubscribe<Integer>) e -> {
            e.onNext(1);
            e.onError(new Exception());
            e.onNext(2);
        }).onExceptionResumeNext(new Observable<Integer>() {
            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                observer.onNext(666);
                observer.onNext(333);
            }
        })
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "integer:" + integer);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "error");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete");
                    }
                });

log

02-03 15:47:35.608 5890-5890/... D/aaabccc: integer:1
02-03 15:47:35.608 5890-5890/... D/aaabccc: integer:666
02-03 15:47:35.608 5890-5890/... D/aaabccc: integer:777
02-03 15:47:35.608 5890-5890/... D/aaabccc: onComplete
//throwable : error
Observable.create((ObservableOnSubscribe<Integer>) e -> {
            e.onNext(1);
            e.onError(new Error());
            e.onNext(2);
        }).onExceptionResumeNext(new Observable<Integer>() {
            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {
                observer.onNext(666);
                observer.onNext(333);
            }
        })
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "integer:" + integer);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "error");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete");

                });

log

02-03 16:24:05.658 11979-11979/... D/aaabccc: integer:1
02-03 16:24:05.658 11979-11979/... D/aaabccc: error
上一篇 下一篇

猜你喜欢

热点阅读