我正在尝试使用 Jsoup 解析网站,获取我获得的信息并填充ListView
. HTML 如下所示:
<ul class="list_view">
<li>
<a href="/username/" >
<table class="pinner">
<tbody>
<tr>
<td class="first_td">
<img src="http://myimgurl.com/img.jpg">
</td>
<td>
<span class="user_name">User Name</span>
</td>
</tr>
</tbody>
</table>
</a>
</li>
</ul>
所以,从这个 HTML 中,我需要href
从 a 标签和 span.user_name 文本中获取。我需要获取这两个元素并将它们存储在一个HashMap
(我认为??)现在,我有一个AsyncTask
这样的(但我不认为我这样做是正确的):
private class MyTask extends AsyncTask<Void, Void, List<HashMap<String, String>>> {
@Override
protected List<HashMap<String, String>> doInBackground(Void... params) {
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
try {
Document doc = Jsoup.connect("http://myurl.com").get();
Elements formalNames = doc.select("li a table tbody tr td span.user_name");
Elements userNames = doc.select("li a");
for (Element formalName : formalNames) {
map.put("col_1", formalName.text());
fillMaps.add(map);
System.out.println(formalName.text());
}
for (Element userName : userNames) {
map.put("col_2", userName.attr("href").toString());
fillMaps.add(map);
System.out.println(userName.attr("href").toString());
}
} catch (IOException e) {
e.printStackTrace();
}
return fillMaps;
}
@Override
protected void onPostExecute(List<HashMap<String, String>> result) {
String[] from = new String[] {"col_1", "col_2"};
int[] to = new int[] { R.id.text1, R.id.text2 };
ListView _listview = (ListView)findViewById(R.id.listView1);
SimpleAdapter _adapter = new SimpleAdapter(FriendsActivity.this, fillMaps, R.layout.friends, from, to);
_listview.setAdapter(_adapter);
}
}
这成功打印出我想要的信息,但它没有填充ListView
. 我曾尝试重新安排等,但仍然没有运气。如果有任何帮助,我将不胜感激。