所以我想從一個論壇網站分析數據,這是我使用的活動代碼:試圖找出這個Android的網絡問題
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listview = (ListView) findViewById(R.id.listView);
final List<String> list = formatTopicData(new TopicRetrievalTask().doInBackground(1));
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
我也有我的異步任務在這裏:
class TopicRetrievalTask extends AsyncTask<Integer, Void, List<TopicListView.TopicData>> {
protected List<TopicListView.TopicData> doInBackground(Integer... page) {
List<TopicListView.TopicData> topics = new ArrayList<TopicListView.TopicData>();
Document doc;
try {
doc = Jsoup.connect("http://<site>/forums/?page=" + page[0])
.userAgent("Mozilla")
.get();
Elements parsed = doc.select("tr[class=topic]");
for (Element topic : parsed) {
TopicListView.TopicData topicData = null;
topicData.setTitle(topic.select("div").select("a").first().text());
topicData.setUrl(topic.select("div").select("a").first().attr("href"));
topicData.setAuthor(topic.select("small").select("a").first().text());
topics.add(topicData);
}
} catch (IOException e) {
e.printStackTrace();
}
return topics;
}
出於某種原因,我仍然得到一個錯誤,說我做的主線程聯網,而我打電話異步任務上線8.任何想法,爲什麼我會得到那?