0

我定义了两个 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)
4

2 回答 2

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

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

于 2013-06-01T20:28:25.917 回答
1
Reader reader = new InputStreamReader(result.getEntity().getContent());

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

于 2013-06-01T20:29:21.223 回答