NetworkOnMainThreadException 与 .subscribeOn(Schedulers.newThread())

NetworkOnMainThreadException with .subscribeOn(Schedulers.newThread())

我正在尝试从 api 中获取列表并使用 Retrofit 和 RxJava 显示在 recyclerview 中。

我使用了下面的代码-

    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();

        }
    });

下面两行指定将在新线程中进行 REST 调用。而当调用响应returns时,会在mainThread上调用onNext、onError、onComplete方法

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

但是我收到 NetworkOnMainThreadException 当应用程序试图在其主服务器上执行网络操作时抛出的 thread.Why 我是否收到此异常以及如何解决这个?

您订阅了两次,第二次是未修改的调用源。你应该有这样的东西:

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();

    }
});

那是因为您订阅了 2 次 observable。您可以从这里删除订阅方法:

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

所以它看起来像这样:

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();

            }
        });