在您悬停关闭按钮之前,请完全阅读此问题。
前言
显然,此异常是由于在主线程上运行网络操作引起的。但是,从堆栈跟踪中可以看出,此错误源自AsyncTask
的doInBackground
!此后我也没有转到主线程。
有趣的是,我无法在我的设备上复制此问题。对于绝大多数用户而言,这也不是一个经常出现的问题。因此,它不能是一般性的实现错误。
步骤
- 主线程
- 某些东西导致频道列表刷新(无论是手动还是初始加载)
- 调用我的API库(附录1)
- 后台线程
AsyncTask
调用API库以获取URL的正文(附录2)- 我的缓存库检查是否已保存正文,它不会更改
Thread
。 - API库调用必要的
OkHttp
方法来拉取URL的正文(附录3)
堆栈跟踪
Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
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 android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
at libcore.io.IoBridge.recvfrom(IoBridge.java:549)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:481)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
at okio.Okio$2.read(SourceFile:139)
at okio.AsyncTimeout$2.read(SourceFile:211)
at okio.RealBufferedSource.indexOf(SourceFile:306)
at okio.RealBufferedSource.indexOf(SourceFile:300)
at okio.RealBufferedSource.readUtf8LineStrict(SourceFile:196)
at okhttp3.internal.http.Http1xStream.readResponse(SourceFile:184)
at okhttp3.internal.http.Http1xStream.readResponseHeaders(SourceFile:125)
at okhttp3.internal.http.HttpEngine.readNetworkResponse(SourceFile:723)
at okhttp3.internal.http.HttpEngine.access$200(SourceFile:81)
at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(SourceFile:708)
at okhttp3.internal.http.HttpEngine.readResponse(SourceFile:563)
at okhttp3.RealCall.getResponse(SourceFile:241)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(SourceFile:198)
at okhttp3.RealCall.getResponseWithInterceptorChain(SourceFile:160)
at okhttp3.RealCall.execute(SourceFile:57)
at com.mypackagename.app.http.Api.getBody(SourceFile:1472)
at com.mypackagename.app.tasks.GetChannelListTask$2.renewCache(SourceFile:72)
at com.mypackagename.app.utils.Cache.get(SourceFile:27)
at com.mypackagename.app.tasks.GetChannelListTask.doInBackground(SourceFile:69)
at com.mypackagename.app.tasks.GetChannelListTask.doInBackground(SourceFile:24)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
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)
附录1
public void getChannelList(final IGetChannelListCallback callback) {
try {
GetChannelListTask task = new GetChannelListTask(mContext.get(), callback);
AsyncTaskCompat.executeParallel(task);
} catch (RejectedExecutionException e) {
Crashlytics.logException(e);
}
}
附录2
protected ArrayList<Channel> doInBackground(String... urls) {
final ArrayList<Channel> channelList = new ArrayList<Channel>();
// ...
String json = Cache.get(context, GET_CHANNELS_CACHE, GET_CHANNELS_CACHE_TIME, new IBadCache() {
public String renewCache() {
return Api.getInstance(context).getBody(GET_CHANNELS_URL);
}
});
// ...
return channelList;
}
附录3
@WorkerThread
@NonNull
public String getBody(@NonNull final String url) {
try {
Request request = new Request.Builder()
.url(logUrl(url))
.build();
Response response = mClient.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
Utils.log("API", "E::"+e.toString());
} catch (SecurityException e) {
reportNoInternetPermission();
}
return "";
}
更新1
检查线程的结果与预期相符:
refreshChannelList: MainThread: true
getChannelList: MainThread: true
onPreExecute: MainThread: true
doInBackground: MainThread: false
renewCache: MainThread: false
getBody: MainThread: false
onPostExecute: MainThread: true
在doInBackground
中切换到后台线程并继续执行OkHttp
调用,在到达onPostExecute
时返回到主线程。
doInBackground
、缓存回调、getBody
等方法的开头和结尾处。你可以使用Thread.currentThread()
来实现。 - nhaarmanmClient.newCall(request).execute()
是否会导致在工作线程上执行而不是主线程上执行的原因。 - Bharatesh