OkHttp 上的不平衡进入/退出
Posted
技术标签:
【中文标题】OkHttp 上的不平衡进入/退出【英文标题】:Unbalanced enter/exit on OkHttp 【发布时间】:2018-12-15 07:53:43 【问题描述】:所以我经常遇到一个例外,即java.lang.IllegalStateException: Unbalanced enter/exit
,当我的活动的onStop
调用disposables.clear()
时,就会发生这种情况。
我asked about it before 被告知这是一个 D8 错误,但我现在使用的是最新的 D8,或者 android Studio 3.1.3 的最新版本,它仍在发生。
我也 asked about it on github for OkHttp 并被告知要找到一种方法来致电 call.cancel()
。
所以我的问题是,我怎样才能让disposables.clear()
调用call.cancel()
?
这是个例外:
Caused by java.lang.IllegalStateException: Unbalanced enter/exit
at okio.AsyncTimeout.enter(AsyncTimeout.java:73)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:235)
at okio.RealBufferedSource.read(RealBufferedSource.java:47)
at okhttp3.internal.http1.Http1Codec$AbstractSource.read(Http1Codec.java:363)
at okhttp3.internal.http1.Http1Codec$ChunkedSource.read(Http1Codec.java:453)
at okio.RealBufferedSource.read(RealBufferedSource.java:47)
at okhttp3.internal.cache.CacheInterceptor$1.read(CacheInterceptor.java:174)
at okhttp3.internal.Util.skipAll(Util.java:175)
at okhttp3.internal.Util.discard(Util.java:157)
at okhttp3.internal.cache.CacheInterceptor$1.close(CacheInterceptor.java:202)
at okio.RealBufferedSource.close(RealBufferedSource.java:469)
at okio.RealBufferedSource$1.close(RealBufferedSource.java:453)
at java.io.BufferedInputStream.close(BufferedInputStream.java:472)
at java.nio.channels.Channels$ReadableByteChannelImpl.implCloseChannel(Channels.java:255)
at java.nio.channels.spi.AbstractInterruptibleChannel$1.interrupt(AbstractInterruptibleChannel.java:166)
at java.lang.Thread.interrupt(Thread.java:956)
at java.util.concurrent.FutureTask.cancel(FutureTask.java:146)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.cancel(ScheduledThreadPoolExecutor.java:258)
at io.reactivex.internal.schedulers.ScheduledRunnable.dispose(ScheduledRunnable.java:107)
at io.reactivex.disposables.CompositeDisposable.dispose(CompositeDisposable.java:217)
at io.reactivex.disposables.CompositeDisposable.dispose(CompositeDisposable.java:80)
at io.reactivex.internal.schedulers.ioscheduler$EventLoopWorker.dispose(IoScheduler.java:210)
at io.reactivex.Scheduler$DisposeTask.dispose(Scheduler.java:464)
at io.reactivex.internal.disposables.DisposableHelper.dispose(DisposableHelper.java:125)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.dispose(ObservableSubscribeOn.java:74)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.dispose(ObservableObserveOn.java:146)
at io.reactivex.internal.disposables.DisposableHelper.dispose(DisposableHelper.java:125)
at io.reactivex.observers.DisposableObserver.dispose(DisposableObserver.java:91)
at io.reactivex.disposables.CompositeDisposable.dispose(CompositeDisposable.java:217)
at io.reactivex.disposables.CompositeDisposable.clear(CompositeDisposable.java:183)
at MyActivity.onStop(MyActivity.java:321)
at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1289)
at android.app.Activity.performStop(Activity.java:6841)
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3947)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4005)
at android.app.ActivityThread.-wrap25(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1561)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
代码基本上是这样的:
Observable<Stuff> observable = Observable.create(new ObservableOnSubscribe<Stuff>()
@Override
public void subscribe(@NonNull ObservableEmitter<Stuff> e) throws Exception
//do OkHttp stuff, call onNext(stuff).
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
DisposableObserver<Stuff> disposableObserver = observable
.subscribeWith(new DisposableObserver<Stuff>() ......);
disposables.add(disposableObserver);
【问题讨论】:
【参考方案1】:e.setCancellable(new Cancellable()
@Override
public void cancel() throws Exception
call.cancel();
);
在subscribe(@NonNull ObservableEmitter<Stuff> e)
方法内部调用Emitter的setCancellable
,这个Cancellable
会在Observable
被释放时执行。
我很好奇你是如何调用close
方法的,在我的情况下,中断线程永远不会调用Source
的close
方法,我必须手动调用call.cancel()
才能中断阻塞阅读。
【讨论】:
以上是关于OkHttp 上的不平衡进入/退出的主要内容,如果未能解决你的问题,请参考以下文章