1

我有一个带有这两个重写方法的 AsyncTask:

@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

现在,当我尝试运行我的软件时,我得到了一个异常。这是日志:

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)

其中 RecoverListTask.java:109 是第一条指令

reader.readLine()

怎么了?

4

2 回答 2

1

文档中

void   onPostExecute(Result result)

之后在 UI 线程上运行doInBackground(Params...)

由于该方法onPostExecute在主线程中执行,因此您无法执行网络操作。因此,由于您BufferedReader已绑定到网络InputStream,因此NetworkOnMainThreadException会提高。

读取onBackground方法中的数据。

于 2013-11-07T21:53:29.613 回答
-2

在 onBackground 上做网络事情,在 onPostExecute 上做活动事情(比如更新视图和显示 Toast 消息)。

于 2013-11-07T20:49:49.897 回答