RxJava onError不能在未调用Looper.prepare()的线程中创建处理程序

时间:2021-05-04 20:44:48

first i will try to explain what im trying to do, next you will see what im doing(code). Since im new at RxJava, and still learning fell free to give me your opinion.

首先我将尝试解释我要做什么,然后你将看到我正在做什么(代码)。因为我刚来RxJava,而且还在学习,所以可以*地给我你的意见。

So, im calling a network API from server and when start request i call loader(spinner), when finish i hide it and same when i get an error. I would like this generic for all my requests so i get Observable and Observer from parameter. On this method, i just care about hide and show loader.

因此,我从服务器调用一个网络API,当启动请求时,我调用loader(spinner),当完成时,我隐藏它,当我得到一个错误时,也是一样。我希望这是我所有请求的通用,这样我就能从参数中得到观察和观察。在这个方法中,我只关心隐藏和显示加载程序。

OnError(and here is the trick part), im trying to show a dialog too, but i got the error that you can see on title. Can't create handler inside thread that has not called Looper.prepare()

OnError(这里是技巧部分),我也试着显示一个对话框,但是我得到了标题上的错误。无法在未调用looper. .prepare()的线程内创建处理程序。

Here is the code..

这是代码. .

protected void makeMyrequest(MyBaseActivity myBaseActivity, Observable observable, Observer observer) {

    mSubscription = observable
            .doOnRequest(new Action1<Long>() {
                @Override
                public void call(Long aLong) {

                    Log.d(TAG, "On request");
                    myBaseActivity.showLoader();
                }
            })
            .doOnCompleted(new Action0() {
                @Override
                public void call() {
                    Log.d(TAG, "onCompleted: Hide spinner");
                    myBaseActivity.hideLoader();
                    mSubscription.unsubscribe();
                }
            })
            .doOnError(new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {

                    Log.d(TAG, "onError: Hide spinner");
                        myBaseActivity.showAlertDialog("error");
                        myBaseActivity.hideLoader();

                                        }
            })
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(observer);
}

On my base activity i have a method to show dialog

在我的基本活动上,我有一个显示对话的方法

public void showAlertDialog(String message) {

    mDialog = new AlertDialog.Builder(this)
            .setMessage(message)
            .show();
}

The part that matters from stacktracer

stacktracer中最重要的部分

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                                                at android.os.Handler.<init>(Handler.java:200)
                                                                                at android.os.Handler.<init>(Handler.java:114)
                                                                                at android.app.Dialog.<init>(Dialog.java:119)
                                                                                at android.app.Dialog.<init>(Dialog.java:168)
                                                                                at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:43)
                                                                                at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:95)
                                                                                at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:927)
                                                                                at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:952)
                                                                                at xx.myapp.MyBaseActivity.showAlertDialog

1 个解决方案

#1


15  

You need to call observeOn(AndroidSchedulers.mainThread()) before doOnRequest. observeOn applies to all operators after him on a chain of calls. In your case exception raised because you are trying to create a dialog outside of main thread.

您需要在doOnRequest之前调用observeOn(android调度程序,mainthread())。observeOn适用于所有在他之后的运营商。在您的案例中,由于您试图在主线程之外创建一个对话框,所以引发了异常。

#1


15  

You need to call observeOn(AndroidSchedulers.mainThread()) before doOnRequest. observeOn applies to all operators after him on a chain of calls. In your case exception raised because you are trying to create a dialog outside of main thread.

您需要在doOnRequest之前调用observeOn(android调度程序,mainthread())。observeOn适用于所有在他之后的运营商。在您的案例中,由于您试图在主线程之外创建一个对话框,所以引发了异常。