运行结果如图所示,但是运行结果不是我想要的,获取的新闻不能显示在listview上,想要的结果类似于那个点菜那样,如图所示
public class MainActivity extends Activity {
Document doc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
load();
}
});
}
protected void load() {
try {
doc = Jsoup.connect("http://www.gdst.cc/News.aspx?id=1").get();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Elements es = doc.getElementsByClass("single_page_list");
for (Element e : es) {
Map<String, String> map = new HashMap<String, String>();
map.put("title", e.getElementsByTag("li").text());
map.put("href", "http://www.gdst.cc/"
+ e.getElementsByTag("li").first().getElementsByTag("a").first().attr("href"));
list.add(map);
}
ListView listView = (ListView) findViewById(R.id.listView1);
SimpleAdapter Adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2,
new String[] { "title","href" }, new int[] {
android.R.id.text1,android.R.id.text2
});
listView.setAdapter(Adapter);
}
/**
* @param urlString
* @return
*/
public String getHtmlString(String urlString) {
try {
URL url = null;
url = new URL(urlString);
URLConnection ucon = null;
ucon = url.openConnection();
InputStream instr = null;
instr = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(instr);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return EncodingUtils.getString(baf.toByteArray(), "gbk");
} catch (Exception e) {
return "";
}
}
}