本文介绍了Observer.onError发射了不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的改造来访问我的API如下:

I am using Retrofit to access my API as follows:

public interface UserService {
    ...
    @POST("/user/login")
    public Observable<User> register(@Body() User user);
}

下面是如何访问我的API:

Here is how I access my API:

mUserService.register(user)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<User>() {
    @Override
    public void onCompleted() {
    ....
    }

    @Override
    public void onError(Throwable e) {
    ....
    }

    @Override
    public void onNext(User user) {
    ....
    }
});

这工作得很好,除非有一个例外(即IOException异常,或当连接超时),则onError方法不被解雇,而不是我得到它终止我的应用程序的主线程上的异常。

This works perfectly well, except when there is an exception (i.e. IOException, or when connection times out), the onError method doesn't get fired, instead I get an exception on the main thread which terminates my application.

然而,对于某些情况下(当API调用,状态code 400的响应,如),该onError方法烧制预期。

However, for some cases (such as when the API call responds with status code 400), the onError method is fired as expected.

挖掘的logcat输出后,我发现这条线,(不知道如何我应该处理这个)

After digging the logcat output, I have spotted this line, (not sure how I am supposed to deal with this)

rx.exceptions.OnErrorFailedException:试图传播错误Observer.onError时发生错误

有人可以让我知道我在做的事情错了?

Can someone let me know where I am doing things wrong?

推荐答案

在我的$ P $与RxJava pvious经验 OnErrorFailedException 表示,虽然处理另一个异常发生异常在的onError 方法。

In my previous experience with RxJava the OnErrorFailedException means that an exception occurred while handling another exception in the onError method.

如果你看不到的真正原因很可能是由于在RxJava错误与 CompositeException (版本0.19.2及以上)堆栈跟踪https://github.com/Netflix/RxJava/issues/1405

If you don't see stack trace of the real cause it's probably due the bug in RxJava with CompositeException (versions 0.19.2 and above) https://github.com/Netflix/RxJava/issues/1405

作为一种变通方法尝试包装你的的onError code在的try-catch 块,并记录异常。这样,你会看到什么是你的的onError 的实施问题,您将能够解决的问题。

As a workaround try to wrap your onError code within try-catch block and log the exception. This way you will see what's the problem with your onError implementation and you will be able to solve the problem.

@Override
public void onError(Throwable e) {
   try {
      ...
   catch(Throwable e) {
      // Log the exception
   }
}

这篇关于Observer.onError发射了不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:13