我正在使用以下(过时)库:
Retrofit: 1.9.0
OkHTTP: 2.3.0
RxAndroid: 0.24.0
我注意到每隔一段时间我就会收到以下用于POST请求的堆栈跟踪:
D/Retrofit: ---> HTTP POST https:xxxxx
D/Retrofit: Content-Type: application/x-www-form-urlencoded; charset=UTF-8
D/Retrofit: Content-Length: 396
D/Retrofit: ---> END HTTP (396-byte body)
D/Retrofit: ---- ERROR https:xxxxx
I/Choreographer: Skipped 33 frames! The application may be doing too much work on its main thread.
D/Retrofit: java.io.InterruptedIOException
at okio.Timeout.throwIfReached(Timeout.java:146)
at okio.Okio$1.write(Okio.java:75)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at okio.RealBufferedSink.flush(RealBufferedSink.java:201)
at com.squareup.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:140)
at com.squareup.okhttp.Connection.makeTunnel(Connection.java:399)
at com.squareup.okhttp.Connection.upgradeToTls(Connection.java:229)
at com.squareup.okhttp.Connection.connect(Connection.java:159)
at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:175)
at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:120)
at com.squareup.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:330)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:319)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
at com.squareup.okhttp.Call.getResponse(Call.java:271)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
at com.squareup.okhttp.Call.execute(Call.java:79)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy1.replyTransaction(Unknown Source)
< App Specific Trace >
at rx.Observable$1.call(Observable.java:145)
at rx.Observable$1.call(Observable.java:137)
at rx.Observable.unsafeSubscribe(Observable.java:7304)
at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
D/Retrofit: ---- END ERROR
retrofit.RetrofitError
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy1.replyTransaction(Unknown Source)
< App Specific Trace >
at rx.Observable$1.call(Observable.java:145)
at rx.Observable$1.call(Observable.java:137)
at rx.Observable.unsafeSubscribe(Observable.java:7304)
at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.io.InterruptedIOException
at okio.Timeout.throwIfReached(Timeout.java:146)
at okio.Okio$1.write(Okio.java:75)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at okio.RealBufferedSink.flush(RealBufferedSink.java:201)
at com.squareup.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:140)
at com.squareup.okhttp.Connection.makeTunnel(Connection.java:399)
at com.squareup.okhttp.Connection.upgradeToTls(Connection.java:229)
at com.squareup.okhttp.Connection.connect(Connection.java:159)
at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:175)
at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:120)
at com.squareup.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:330)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:319)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
at com.squareup.okhttp.Call.getResponse(Call.java:271)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
at com.squareup.okhttp.Call.execute(Call.java:79)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at java.lang.reflect.Proxy.invoke(Proxy.java:913)
at $Proxy1.replyTransaction(Unknown Source)
< App Specific Trace >
at rx.Observable$1.call(Observable.java:145)
at rx.Observable$1.call(Observable.java:137)
at rx.Observable.unsafeSubscribe(Observable.java:7304)
at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
我的代码如下:
网络电话:
return Observable.create(new Observable.OnSubscribe<Object>() {
@Override public void call(Subscriber<? super Object> subscriber) {
try {
subscriber.onNext(/* Sync network call here. */);
} catch (Exception e) {
subscriber.onError(e);
}
subscriber.onCompleted();
}
})
.onBackpressureBuffer()
// singleton for Schedulers.io()
.subscribeOn(ioScheduler)
// singleton for AndroidSchedulers.mainThread()
.observeOn(mainThreadScheduler);
这样称呼:
subscription = makeNetworkCall()
.subscribe(new Observer<Object>() {
@Override public void onNext(Object object) {
// close and finish activity
}
@Override public void onError(Throwable e) {
// whoops!
}
});
并且在活动中取消订阅如下:
@Override
protected void onPause() {
super.onPause();
if (subscription != null) {
subscription.unsubscribe();
subscription = null;
if (condition) {
finish();
}
}
}
我对此的理解是,我收到了InterruptedException
,因为我unsubscribe()
来自Subscription
onPause()
。我们这样做是为了其他请求,所以我不确定为什么我只在这里看到它以及为什么会发生这种情况。
为了提供更多相关信息,网络呼叫从通过通知操作从锁定屏幕启动的活动发生,以强制用户解锁其设备以采取行动。
我的问题是为什么会发生这种情况并且有一个很好的方法来修复它?根据一些文章,onPause()
是您应该取消订阅Observables
的地方。
有关这方面的更多信息,请参阅In okHTTP's github issues
谢谢!
答案 0 :(得分:1)
取消订阅后,链条不再需要做任何工作,因为我们不再关心输出。我们可以让当前的操作完成,当我们到达下一个操作员时干净地停止工作,但为什么要浪费我们不需要的任何资源?如果有正在进行的网络请求,我们要取消它。我们通过中断线程来做到这一点。如果/当此线程上的代码检查它是否被中断时,则不需要再进行任何工作,并且可以抛出InterruptedIOException
。
根据确切调用取消订阅的时间,您可能会看到错误,也可能看不到错误。如果在请求开始加载之前或者在完成请求之后调用取消订阅,那么您可能看不到此错误。
所以这个例外的原因是OkHttp想要一旦知道你对它的结果不感兴趣就放弃了,而RxJava并不想吞下它,以防它对你很重要。
您可以添加默认错误处理程序来抑制无法传递的InterruptedException
和InterruptedIOException
,这些很少有用。请参阅RxJava 2 Error Handling,我相信RxJava 1有一个类似的构造,但你应该考虑迁移到RxJava 2.它真的不是很大的离开,并且RxBinding已经更新为RxJava 2。
onPause()
不一定是取消订阅的地方。您应该取消订阅应用程序逻辑所需的位置以及防止内存泄漏的位置。
答案 1 :(得分:0)
这是猜测,但可能是您在发出中断的异常之前没有检查订户是否仍在那里。
试试这个:
return Observable.create(new Observable.OnSubscribe<Object>() {
@Override public void call(Subscriber<? super Object> subscriber) {
try {
subscriber.onNext(/* Sync network call here. */);
subscriber.onCompleted();
} catch (Exception e) {
if (!subscriber.isUnsubscribed()) { // <-- check before emitting error
subscriber.onError(e);
}
}
}
})
答案 2 :(得分:0)
试试这个:
return Observable.create(e -> {
try {
e.onNext(...);
} catch (Exception ex) {
e.tryOnError(ex);
}
e.onComplete();
});
tryOnError()检查是发送错误时是否处置Observer。