我有一个带有这两个重写方法的 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()
怎么了?