【发布时间】:2016-06-10 15:25:54
【问题描述】:
我知道网络任务应该在异步线程中完成,我认为我的代码在一个异步线程中,但我仍然收到错误
.MainActivity}: android.os.NetworkOnMainThreadException
这让我很困惑,因为几乎所有内容都在异步任务中:
public void onStart() {
super.onStart();
new GetRssFeedTask().execute();
}
其余代码在异步任务中:
private class GetRssFeedTask extends AsyncTask<Void, Void, List<String>> {
private String getRssFeed() throws IOException {
InputStream in = null;
String rssFeed = null;
try {
URL url = new URL("http://stuffilikenet.wordpress.com/feed/main.xml");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int count; (count = in.read(buffer)) != -1; ) {
out.write(buffer, 0, count);
}
byte[] response = out.toByteArray();
rssFeed = new String(response, "UTF-8");
} finally {
if (in != null) {
in.close();
}
}
return rssFeed;
}
...rest of code (seriously)...
}
我应该在哪里查找我的错误?
【问题讨论】:
-
发布您的
doInBackground()方法。 -
您的代码应该在 doInBackground() 中,而不仅仅是您创建的某个方法
标签: android asynchronous networking android-asynctask