BufferedReader.readLine()将函数传递给函数时发生异常

时间:2013-11-07 20:46:32

标签: java android android-asynctask bufferedreader

我有一个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()

怎么了?

2 个答案:

答案 0 :(得分:1)

来自the documentation

void   onPostExecute(Result result)
     

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

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

阅读onBackground方法中的数据。

答案 1 :(得分:-2)

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