1

I have an AsyncTask with this two overrided methods:

@Override
protected BufferedReader doInBackground(Void... arg0) {
    if (externalStorageWriteable) {

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(link_to_list);
        HttpResponse response;
        try {
            response = client.execute(request);
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in));

            return reader;
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } else {
        Toast.makeText(context, "Unable to write on external storage",
                Toast.LENGTH_LONG).show();
    }

    return null;
}

/*
 * Modify adapter with retrieved data from html page
 */
@Override
protected void onPostExecute(BufferedReader reader) {
    if (externalStorageWriteable) {
        // parsing bufferedreader content
        try {
            // skip first 3 rows of document. Contains unuseful data
            reader.readLine();
            reader.readLine();
            reader.readLine();
                            //other code

Now when i try to run my software, i obtain an exception. This is the log:

11-07 21:36:08.853: E/AndroidRuntime(24155): FATAL EXCEPTION: main
11-07 21:36:08.853: E/AndroidRuntime(24155): android.os.NetworkOnMainThreadException
11-07 21:36:08.853: E/AndroidRuntime(24155):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at org.apache.http.impl.io.SocketInputBuffer.isDataAvailable(SocketInputBuffer.java:80)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at org.apache.http.impl.io.IdentityInputStream.available(IdentityInputStream.java:63)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at org.apache.http.conn.EofSensorInputStream.available(EofSensorInputStream.java:196)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at java.io.InputStreamReader.read(InputStreamReader.java:234)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at java.io.BufferedReader.readLine(BufferedReader.java:354)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at com.example.podcast610downloader.RecoverListTask.onPostExecute(RecoverListTask.java:109)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at com.example.podcast610downloader.RecoverListTask.onPostExecute(RecoverListTask.java:1)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at android.os.AsyncTask.finish(AsyncTask.java:631)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at android.os.Looper.loop(Looper.java:137)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at android.app.ActivityThread.main(ActivityThread.java:5103)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at java.lang.reflect.Method.invokeNative(Native Method)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at java.lang.reflect.Method.invoke(Method.java:525)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 21:36:08.853: E/AndroidRuntime(24155):    at dalvik.system.NativeStart.main(Native Method)

where RecoverListTask.java:109 is the first instruction

reader.readLine()

What's wrong?

giozh
  • 9,868
  • 30
  • 102
  • 183
  • move that stuff to doInBackground, onPostExecute is no place for that. – petey Nov 07 '13 at 20:57
  • possible duplicate of [NetworkOnMainThreadException](http://stackoverflow.com/questions/5150637/networkonmainthreadexception) – user207421 Nov 07 '13 at 21:00
  • why? doInBackGround it's used to make the "long" task, and onPostExecute for use recovered data of doInBackground. This is what i'm exactly doing! – giozh Nov 07 '13 at 21:01

2 Answers2

1

From the documentation:

void   onPostExecute(Result result)

Runs on the UI thread after doInBackground(Params...).

Since the method onPostExecute is executed in the main thread you cannot execute network operations. So, since your BufferedReader is bound to a network InputStream the NetworkOnMainThreadException is raised.

Read the data in the onBackground method.

Community
  • 1
  • 1
Santipeppe
  • 11
  • 1
-2

Do network stuff on the onBackground, do activity stuff (like updating views and showing Toast messages) in onPostExecute.

wvdz
  • 16,251
  • 4
  • 53
  • 90
  • My answer is actually the correct answer now. It seems you don't really understand how AsyncTask works. – wvdz Nov 07 '13 at 21:05
  • No, you're reading the InputStream in the onPost, which is a network operation. – wvdz Nov 07 '13 at 21:06