The exception was not handled due to missing onError handler in the subscribe() method call

The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | cn.leancloud.AVException

出现这个问题,是因为在使用Rxjava的时候,没有捕获异常,rxjava捕获异常有两种方式:
1、全局捕获rxjava异常

在自定义Application的onCreate方法中加入RxJavaPlugins.setErrorHandler这个方法

RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
    @Override
    public void accept(Throwable throwable) throws Exception {
        throwable.printStackTrace();//这里处理所有的Rxjava异常
    }
});

2、使用subscribe的下面构造方法捕获异常

@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError) {
    return subscribe(onNext, onError, Functions.EMPTY_ACTION, Functions.emptyConsumer());
}
@SchedulerSupport(SchedulerSupport.NONE)

@Override
public final void subscribe(Observer<? super T> observer) {
}

例如:

Observable.create(new ObservableOnSubscribe<String>() {
    @Override
    public void subscribe(ObservableEmitter<String> emitter) throws Exception {
        emitter.onNext("");
        emitter.onComplete();
    }
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
    @Override
    public void onSubscribe(Disposable d) {
    }

    @Override
    public void onNext(String result) {
    }

    @Override
    public void onError(Throwable e) {//处理异常
        e.printStackTrace();
    }

    @Override
    public void onComplete() {
    }
});
Observable.create(new ObservableOnSubscribe<String>() {
    @Override
    public void subscribe(ObservableEmitter<String> emitter) throws Exception {
        emitter.onNext("");
        emitter.onComplete();
    }
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
   @Override
    public void accept(String s) throws Exception {

    }
}, new Consumer<Throwable>() {
    @Override
    public void accept(Throwable throwable) throws Exception {//处理异常
        throwable.printStackTrace();
    }
});

你可能感兴趣的:(Android)