在MainActivity7
2.讲解
需要在emitter.onError里面执行抛异常,这样才能抛异常,不报错。如
emitter.onError(new IllegalAccessException("我要报错了,你小心点,哈哈哈!"));
1.onErrorReturn
这个报错后,下游不会接收到消息,可以发射一个错误提示给下游,如发射一个404
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Throwable {
for (int i = 0; i < 100; i++) {
if (i == 5) {
emitter.onError(new IllegalAccessException("我要报错了,你小心点,哈哈哈!"));
}
emitter.onNext(i);
Log.d(TAG, "subscribe: "+i);
}
emitter.onComplete();
}
})
.onErrorReturn(new Function<Throwable, Integer>() {
@Override
public Integer apply(Throwable throwable) throws Throwable {
Log.d(TAG, "apply: "+throwable.getMessage());
return 400;
}
})
.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.d(TAG, "onSubscribe: ");
}
@Override
public void onNext(@NonNull Integer integer) {
Log.d(TAG, "onNext: "+integer);
}
@Override
public void onError(@NonNull Throwable e) {
Log.d(TAG, "onError: ");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete: ");
}
});
2.onErrorResumeNext
错误发射后,可以创建一个被观察者,可以继续发射emitter.onNext(400);
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Throwable {
for (int i = 0; i < 100; i++) {
if (i == 5) {
emitter.onError(new Error("错错错"));
}else {
emitter.onNext(i);
Log.d(TAG, "subscribe: "+i);
}
}
emitter.onComplete();
}
})
.onErrorResumeNext(new Function<Throwable, ObservableSource<? extends Integer>>() {
@Override
public ObservableSource<? extends Integer> apply(Throwable throwable) throws Throwable {
return Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Throwable {
emitter.onNext(400);
emitter.onNext(400);
emitter.onNext(400);
emitter.onNext(400);
emitter.onNext(400);
emitter.onNext(400);
emitter.onComplete();
}
});
}
})
.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.d(TAG, "onSubscribe: ");
}
@Override
public void onNext(@NonNull Integer integer) {
Log.d(TAG, "onNext: "+integer);
}
@Override
public void onError(@NonNull Throwable e) {
Log.d(TAG, "onError: ");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete: ");
}
});
3.onErrorResumeWith
感觉跟onErrorResumeNext差不多,可以继续发射
4.retry
表示报错后,重试。其中true表示重试,false表示不重试。一般都用true吧
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Throwable {
for (int i = 0; i < 100; i++) {
if (i == 5) {
emitter.onError(new IllegalAccessException("错错错"));
}else {
emitter.onNext(i);
Log.d(TAG, "subscribe: "+i);
}
}
emitter.onComplete();
}
})
.retry(new BiPredicate<Integer, Throwable>() {
@Override
public boolean test(@NonNull Integer integer, @NonNull Throwable throwable) throws Throwable {
Thread.sleep(2);
Log.d(TAG, "test重试了: "+integer+"次"+throwable.getMessage());
return true;
}
})
.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.d(TAG, "onSubscribe: ");
}
@Override
public void onNext(@NonNull Integer integer) {
Log.d(TAG, "onNext: "+integer);
}
@Override
public void onError(@NonNull Throwable e) {
Log.d(TAG, "onError: ");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete: ");
}
});
1.retry()
表示一直重试
2.retry(new Predicate()
表示重试一段时间后,再停止
3.retry(3, new Predicate()
表示可以设置重试的次数,这里设置为重试3次
4.retry(new BiPredicate<Integer, Throwable>()
重试多次,然后打印重试的次数
|