带有Retrofit 2和RxJava的concatMap()中的NetworkOnMainThreadException

时间:2015-11-28 16:52:18

标签: android retrofit rx-java

在我的应用程序中,当用户搜索新电影并从Web服务检索电影数据时,我正在尝试使用RxJava做出反应。 为了做到这一点,我想使用concatMap连接获取新电影查询到请求电影到web服务的操作。 这样做我得到一个NetworkOnMainThread异常,我不明白原因..

 createSearchViewObservable(searchView)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .concatMap(new Func1<String, Observable<MoviesWrapper>>() {
                @Override
                public Observable<MoviesWrapper> call(String title) {
                    RestMovieSource repo = new RestMovieSource();
                   return repo.searchMovieByTitle(title);
                }
            })
            .subscribe(new Subscriber<MoviesWrapper>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), e.getCause().getMessage(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onNext(MoviesWrapper moviesWrapper) {
                    for (Movie movie : moviesWrapper.getResults()) {
                        Toast.makeText(getContext(), movie.getTitle(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
D/OkHttp: --> GET /3/search/movie?api_key=xxxx&query=Jurassic%20World HTTP/1.1
D/OkHttp: --> END GET
  android.os.NetworkOnMainThreadException
      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
      at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
      at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
      at java.net.InetAddress.getAllByName(InetAddress.java:215)
      at com.squareup.okhttp.Dns$1.lookup(Dns.java:39)
      at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:184)
      at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:153)
      at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:95)
      at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
      at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
      at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
      at com.squareup.okhttp.Call.getResponse(Call.java:276)
      at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:234)
      at com.squareup.okhttp.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:180)
      at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:223)
      at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:196)
      at com.squareup.okhttp.Call.execute(Call.java:79)
      at retrofit.OkHttpCall.execute(OkHttpCall.java:116)
      at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:111)
      at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:88)
      at rx.Observable$2.call(Observable.java:162)
      at rx.Observable$2.call(Observable.java:154)
      at rx.Observable$2.call(Observable.java:162)
      at rx.Observable$2.call(Observable.java:154)
      at rx.Observable.unsafeSubscribe(Observable.java:8171)
      at rx.internal.operators.OperatorConcat$ConcatSubscriber.subscribeNext(OperatorConcat.java:172)
      at rx.internal.operators.OperatorConcat$ConcatSubscriber.onNext(OperatorConcat.java:136)
      at rx.internal.operators.OperatorConcat$ConcatSubscriber.onNext(OperatorConcat.java:79)
      at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:202)
      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:162)
      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5221)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

3 个答案:

答案 0 :(得分:10)

我解决了从concatMap()中为返回的observable定义subscribeOn和observeOn的问题。 subscribeOn / observeOn外部仅特定于createSearchViewObservable(searchView)中的第一个observable。

 createSearchViewObservable(searchView)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .concatMap(new Func1<String, Observable<MoviesWrapper>>() {
            @Override
            public Observable<MoviesWrapper> call(String title) {
                RestMovieSource repo = new RestMovieSource();
               return repo.searchMovieByTitle(title)
                      .subscribeOn(Schedulers.io())  
                      .observeOn(AndroidSchedulers.mainThread());;
            }
        })
        .subscribe(...)

答案 1 :(得分:2)

subscribeOn()之后调用concatMap()

createSearchViewObservable(searchView)
        .concatMap(new Func1<String, Observable<MoviesWrapper>>() {
            @Override
            public Observable<MoviesWrapper> call(String title) {
                RestMovieSource repo = new RestMovieSource();
                return repo.searchMovieByTitle(title);
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<MoviesWrapper>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                Toast.makeText(getContext(), e.getCause().getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(MoviesWrapper moviesWrapper) {
                for (Movie movie : moviesWrapper.getResults()) {
                    Toast.makeText(getContext(), movie.getTitle(), Toast.LENGTH_SHORT).show();
                }
            }
        });

答案 2 :(得分:0)

ConcatMap或flatMap将生成一个新的observable,它从较旧的observable中获取数据。 因此,您要将调度程序设置为较旧的observable,而不是新的observable。