gpt4 book ai didi

android - OkHttp 上的不平衡进入/退出

转载 作者:行者123 更新时间:2023-12-03 17:32:37 26 4
gpt4 key购买 nike

所以我经常有一个异常(exception),说 java.lang.IllegalStateException: Unbalanced enter/exitonStop我的 Activity 电话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() ?

这是一个异常(exception):

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

最佳答案

e.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
call.cancel();
}
});
调用 setCancellable subscribe(@NonNull ObservableEmitter<Stuff> e) 内的发射器方法,这个 Cancellable将在 Observable 时执行被处置。
我很好奇您是如何获得 close 的调用的方法,在我的情况下,中断线程永远不会调用 close Source 的方法,我必须调用 call.cancel()手动中断阻塞读数。

关于android - OkHttp 上的不平衡进入/退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51214905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com