当前位置: 首页 > 知识库问答 >
问题:

RxTextView文本更改+改装调用导致InterruptedIOException

陆博易
2023-03-14

许多带有rxtextview.textchanges示例和debounce的RxJava教程都使用“实时搜索”。例如:用RXJava改进UX。因此,我实现了这个示例,并试着玩了一下:

RxTextView.textChanges(searchView)
          .observeOn(Schedulers.io())
          .skip(1)
          .debounce(DELAY_BEFORE_REQUEST_MS, TimeUnit.MILLISECONDS)
          .map(new Func1<CharSequence, String>() {
                @Override public String call(CharSequence charSequence) {
                    return charSequence.toString();
                }
            })
          .switchMap(new Func1<String, Observable<Response>>() {
                @Override public Observable<Response> call(String query) {
                    return retrofitService.search(query);
                }
            })
          .subscribe();
java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:60)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
      at java.lang.Thread.run(Thread.java:818)
   Caused by: rx.exceptions.OnErrorNotImplementedException: thread interrupted
      at rx.Observable$27.onError(Observable.java:7923)
      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:159)
      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
      at rx.internal.operators.OperatorSubscribeOn$1$1$1.onError(OperatorSubscribeOn.java:71)
      at rx.observers.SerializedObserver.onError(SerializedObserver.java:159)
      at rx.observers.SerializedSubscriber.onError(SerializedSubscriber.java:79)
      at rx.internal.operators.OperatorSwitch$SwitchSubscriber.error(OperatorSwitch.java:223)
      at rx.internal.operators.OperatorSwitch$InnerSubscriber.onError(OperatorSwitch.java:282)
      at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:240)
      at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:776)
      at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:537)
      at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:526)
      at rx.internal.operators.OperatorMerge$MergeSubscriber.onError(OperatorMerge.java:250)
      at rx.internal.operators.OperatorMap$1.onError(OperatorMap.java:48)
      at retrofit2.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:114)
      at retrofit2.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:8098)
      at rx.internal.operators.OperatorSwitch$SwitchSubscriber.onNext(OperatorSwitch.java:105)
      at rx.internal.operators.OperatorSwitch$SwitchSubscriber.onNext(OperatorSwitch.java:60)
      at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
      at rx.internal.operators.OperatorDoOnEach$1.onNext(OperatorDoOnEach.java:85)
      at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
      at rx.observers.SerializedObserver.onNext(SerializedObserver.java:95)
      at rx.observers.SerializedSubscriber.onNext(SerializedSubscriber.java:95)
      at rx.internal.operators.OperatorDebounceWithTime$DebounceState.emit(OperatorDebounceWithTime.java:132)
      at rx.internal.operators.OperatorDebounceWithTime$1$1.call(OperatorDebounceWithTime.java:79)
      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
      at java.lang.Thread.run(Thread.java:818) 
   Caused by: java.io.InterruptedIOException: thread interrupted
      at okio.Timeout.throwIfReached(Timeout.java:145)
      at okio.Okio$2.read(Okio.java:136)
      at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
      at okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
      at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
      at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
      at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
      at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
      at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:723)
      at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:81)
      at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:708)
      at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:563)
    at okhttp3.RealCall.ge

是否有一种方法可以忽略第一个API调用结果,并使用RXJava启动新的API调用结果?或者我应该尝试切换新的改型调用API,并尝试取消以前的请求(并打破一个反应性的方法)?

我已经使用了改版2 beta 3,与最新的Okio和OKHTTP。

共有1个答案

王才英
2023-03-14

这个错误非常明显,您应该添加onerror处理。它可能看起来像这样:

.subscribe(new Observer<Response>() {
    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
    }

    @Override
    public void onNext(Response response) {
    }
});

但是,一旦发出错误,您的订阅将被终止,但您可以通过如下方式处理API调用的错误来避免这种情况:

.switchMap(new Func1<String, Observable<Response>>() {
    @Override public Observable<Response> call(String query) {
        return retrofitService.search(query)
                .onErrorResumeNext(Observable.<Response>empty());
    }
})
 类似资料:
  • java.lang.nosuchmethoderror:org.springframework.boot.builder.springapplicationbuilder.([ljava/lang/object;)V在org.springframework.cloud.bootstrap.bootstrapapplicationlistener.bootstrapserviceContext(bo

  • 我现在有一段html,它代表我的导航栏的相关部分: 我有一个css,我希望用它来改变导航栏的文本颜色: 唯一的问题是文本颜色保持不变。我还看到一个非常相似的问题没有解决。我打赌谁能解决这个问题,谁就能解决另一个问题。

  • HTTP头所在的位置 现在在Spring 5上-这被更改为 现在头从-{accept=[application/json],content-type=[multipart/form-data;boundary=8hfyfdj_y58snxrsdxenwliqdsyixs50],content-length=[51024]} 我搜索了很多,发现了一个类似的问题,这似乎更密切相关--升级到Spring

  • 我克隆了一个flutter项目,并试图在android设备上运行它。但我遇到了这个错误: 任务': app: check DebugAarMetadata'执行失败。发生多个任务操作失败:执行com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction时发生失败 该错误提到当前的compileSdkVersion为30,但某些

  • 当运行我的android项目时,如何知道这个错误的原因?无法确定 null

  • 我正在使用bootstrap制作一个带有bootstrap的网站,这时我遇到了一个小问题。我试图改变我的导航栏(药丸)中文本的颜色: HTML: CSS: (这段代码解决了我的问题,但我有以下几个问题。) 我想知道为什么在CSS中我必须引用。nav类(为什么不只是.nav),以及为什么我必须引用a(链接标记)才能更改文本的颜色。(当我尝试将a(链接标记)更改为li(列表标记)时,文本的颜色没有更改