因此,在这里查看了解决同一问题的其他答案后,这些解决方案似乎不起作用。这是我得到的错误。我在newThread()
上实现了RxJava,在mainThread()
上实现了Observer。
所以我的第一个可观测到的是发射一个可观测的>。我使用flatMapObservable
迭代它,并使用每个整数将单个新闻项作为Observable。
查看我的Http日志,我得到了整数列表和第一条新闻。然后我就犯了一个错误。
如有任何帮助,我们将不胜感激。
mMainProvider.getNewestHackerStories()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(Throwable::printStackTrace)
.flatMapIterable(ids -> ids)
.flatMap(id -> mMainProvider.getHackerNewsItem(id))
.subscribe(hackerNewsItem -> newStoryList.add(hackerNewsItem));
17-12017/com.androidtitan.magicmirror W/System.err: android.os.NetworkOnMainThreadException
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.shutdownAndFreeSslNative(OpenSSLSocketImpl.java:1237)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:1232)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.internal.Util.closeQuietly(Util.java:105)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.internal.http.StreamAllocation.deallocate(StreamAllocation.java:259)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.internal.http.StreamAllocation.streamFailed(StreamAllocation.java:306)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.internal.http.HttpEngine.close(HttpEngine.java:478)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.RealCall.getResponse(RealCall.java:273)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at okhttp3.RealCall.execute(RealCall.java:57)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.Subscriber.setProducer(Subscriber.java:211)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.Subscriber.setProducer(Subscriber.java:205)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.Observable.unsafeSubscribe(Observable.java:8666)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:250)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:147)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.internal.operators.OperatorMap$MapSubscriber.onNext(OperatorMap.java:74)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.internal.operators.OnSubscribeFlattenIterable$FlattenIterableSubscriber.drain(OnSubscribeFlattenIterable.java:239)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.internal.operators.OnSubscribeFlattenIterable$FlattenIterableSubscriber.onNext(OnSubscribeFlattenIterable.java:130)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:227)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at android.os.Looper.loop(Looper.java:158)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7224)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at java.lang.reflect.Method.invoke(Native Method)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
08-04 21:27:16.766 12017-12017/com.androidtitan.magicmirror W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
observeOn(AndroidSchedulers.mainThread())
之后的所有内容都将在主线程上运行。我假设mMainProvider.getHackerNewsItem
是一个网络呼叫,所以您应该将observeOn
线路移到subscribe
之前。