简体   繁体   English

带有.subscribeOn(Schedulers.newThread())的NetworkOnMainThreadException

[英]NetworkOnMainThreadException with .subscribeOn(Schedulers.newThread())

I am trying to fetch list from an api and display in recyclerview using Retrofit with RxJava. 我正在尝试从api中获取列表,并使用带有RxJava的Retrofit在recyclerview中显示。

I have used below code- 我用下面的代码-

    ApiInterface apiService =
            ApiService.getClient().create(ApiInterface.class);

    Observable<MoviesResponse> call = apiService.getTopRatedMovies(API_KEY);
    call.subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe();
    call.subscribe(new Observer<MoviesResponse>() {
        @Override
        public void onSubscribe(Disposable d) {
            Toast.makeText(getApplicationContext(), d.toString(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onNext(MoviesResponse moviesResponse) {
            movies=moviesResponse;
            moviesAdapter.notifyDataSetChanged();
        }

        @Override
        public void onError(Throwable e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onComplete() {
            Toast.makeText(getApplicationContext(), "complete", Toast.LENGTH_LONG).show();

        }
    });

Below two lines specify that the REST call will be made in a new thread . 以下两行指定了将在新线程中进行REST调用。 And when the call response returns, the onNext, onError, and onComplete methods are called on the mainThread. 当调用响应返回时,在mainThread上调用onNext,onError和onComplete方法。

.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())

But I am getting NetworkOnMainThreadException which is thrown when an application attempts to perform a networking operation on its main thread.Why am i getting this exception and how can i resolve this? 但是我收到了NetworkOnMainThreadException ,当应用程序尝试在其主线程上执行网络操作时会抛出该异常,为什么会出现此异常以及如何解决呢?

You are subscribing twice, the second time to the unmodified call source. 您要订阅两次,第二次订阅未修改的呼叫源。 You should have something like this: 您应该具有以下内容:

ApiInterface apiService =
        ApiService.getClient().create(ApiInterface.class);

Observable<MoviesResponse> call = apiService.getTopRatedMovies(API_KEY);

call.subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<MoviesResponse>() {
    @Override
    public void onSubscribe(Disposable d) {
        Toast.makeText(getApplicationContext(), d.toString(), Toast.LENGTH_LONG).show();

    }

    @Override
    public void onNext(MoviesResponse moviesResponse) {
        movies=moviesResponse;
        moviesAdapter.notifyDataSetChanged();
    }

    @Override
    public void onError(Throwable e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();

    }

    @Override
    public void onComplete() {
        Toast.makeText(getApplicationContext(), "complete", Toast.LENGTH_LONG).show();

    }
});

That's because you are subscribing to the observable 2 times. 那是因为您正在订阅可观察的2次。 You can remove subscribe method from here: 您可以从此处删除订阅方法:

call.subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe();

So it will look like this: 所以它看起来像这样:

ApiInterface apiService =
            ApiService.getClient().create(ApiInterface.class);

   call.subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<MoviesResponse>() {
            @Override
            public void onSubscribe(Disposable d) {
                Toast.makeText(getApplicationContext(), d.toString(), Toast.LENGTH_LONG).show();

            }

            @Override
            public void onNext(MoviesResponse moviesResponse) {
                movies=moviesResponse;
                moviesAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();

            }

            @Override
            public void onComplete() {
                Toast.makeText(getApplicationContext(), "complete", Toast.LENGTH_LONG).show();

            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM