Android:执行两个asynctasks时出现NetworkOnMainThreadException

时间:2013-06-01 20:02:21

标签: android android-asynctask

我定义了两个asynctask类。因为我需要在第一个完成后执行第二个,所以我打电话给

new SecondTask(...).execute();

在第一个任务的OnPostExecute()中。

然而,出现“android.os.NetworkOnMainThreadException”。为什么呢?

更新一些代码:

private class FirstTask extends AsyncTask<Void, Void, HttpResponse> {
    ...
    @Override
    protected HttpResponse doInBackground(Void... arg0) {
        ...
    }
    @Override
    protected void onPostExecute(HttpResponse result) {
        Reader reader = new InputStreamReader(result.getEntity().getContent());
        ...
        HttpUriRequest arg0 = new HttpGet(...);
        new SecondTask(arg0).execute();
    }
}

private class SecondTask extends AsyncTask<Void, Void, HttpResponse> {

    private HttpUriRequest request;

    public ClickRequest(HttpUriRequest req) {
        this.request = req;
    }

    @Override
    protected HttpResponse doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        try {
            HttpResponse response = httpClient.execute(request);
            return response;
        } catch (IOException e) {
            return null;
        }
    }
}

堆栈追踪:

06-01 20:19:21.726: D/AdlibView(1241): android.os.NetworkOnMainThreadException
06-01 20:19:21.726: D/AdlibView(1241):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-01 20:19:21.726: D/AdlibView(1241):  at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
06-01 20:19:21.726: D/AdlibView(1241):  at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
06-01 20:19:21.726: D/AdlibView(1241):  at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
06-01 20:19:21.726: D/AdlibView(1241):  at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
06-01 20:19:21.726: D/AdlibView(1241):  at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
06-01 20:19:21.726: D/AdlibView(1241):  at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
06-01 20:19:21.726: D/AdlibView(1241):  at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
06-01 20:19:21.726: D/AdlibView(1241):  at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:220)
06-01 20:19:21.726: D/AdlibView(1241):  at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:183)
06-01 20:19:21.726: D/AdlibView(1241):  at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:155)
06-01 20:19:21.726: D/AdlibView(1241):  at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159)
06-01 20:19:21.726: D/AdlibView(1241):  at java.io.InputStreamReader.read(InputStreamReader.java:244)
06-01 20:19:21.726: D/AdlibView(1241):  at java.io.Reader.read(Reader.java:145)
06-01 20:19:21.726: D/AdlibView(1241):  at edu.stanford.cs.adlib.AdLibView$1.onPostExecute(AdLibView.java:41)
06-01 20:19:21.726: D/AdlibView(1241):  at edu.stanford.cs.adlib.AdLibView$1.onPostExecute(AdLibView.java:1)
06-01 20:19:21.726: D/AdlibView(1241):  at android.os.AsyncTask.finish(AsyncTask.java:631)
06-01 20:19:21.726: D/AdlibView(1241):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-01 20:19:21.726: D/AdlibView(1241):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-01 20:19:21.726: D/AdlibView(1241):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 20:19:21.726: D/AdlibView(1241):  at android.os.Looper.loop(Looper.java:137)
06-01 20:19:21.726: D/AdlibView(1241):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-01 20:19:21.726: D/AdlibView(1241):  at java.lang.reflect.Method.invokeNative(Native Method)
06-01 20:19:21.726: D/AdlibView(1241):  at java.lang.reflect.Method.invoke(Method.java:511)
06-01 20:19:21.726: D/AdlibView(1241):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-01 20:19:21.726: D/AdlibView(1241):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-01 20:19:21.726: D/AdlibView(1241):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

protected void onPostExecute(HttpResponse result) {
    Reader reader = new InputStreamReader(result.getEntity().getContent());

您正在从HttpResponse读取数据。这是主线程。您应该将所有与网络相关的代码移出onPostExecute()

答案 1 :(得分:1)

Reader reader = new InputStreamReader(result.getEntity().getContent());

此操作以及与InputStream相关的所有操作都需要在doInBackground

中运行