0

I define two asynctask classes. Because I need the second one executed after the first one finishes, I call

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

in the first task's OnPostExecute().

However, "android.os.NetworkOnMainThreadException" appears. Why?

Update some codes:

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

Stack Trace:

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)
yoarcher
  • 5
  • 1
  • 3

2 Answers2

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

You are reading data from the HttpResponse here. This is in the main thread. You should move all network-related code out of onPostExecute().

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
1
Reader reader = new InputStreamReader(result.getEntity().getContent());

this operation and all the operations related to the InputStream need to run inside doInBackground

Blackbelt
  • 156,034
  • 29
  • 297
  • 305