Jsoup是通过解析网站网页,结合标签,进而获取想要的内容。
通过继承Asynctask,通过Jsoup解析标签,将标签内容显示出来。
用法很简单。
<span style="font-size:18px;">class LoadHtml extends AsyncTask<String, String, String> {
Document doc;
URLImageParser imageParser = new URLImageParser();
@Override
protected String doInBackground(String... params) { //后台执行的方法
try {
doc = Jsoup.connect(params[0]).get();
Document content = Jsoup.parse(doc.toString());//返回一个Document对象,像JQuery那样解析
Element elementContent = content.getElementById("vsb_content");
if (elementContent == null) {
elementContent = content.getElementById("vsb_content_2");
}
String temp = elementContent.html();
author = content.getElementsByClass("authorstyle44518").text();
time = content.getElementsByClass("timestyle44518").text();
desc = android.text.Html.fromHtml(temp, imageParser, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {//<span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace, 'Microsoft YaHei'; font-size: 14px; line-height: 24px; white-space: pre-wrap;">onPostExecute方法是在doInBackground方法执行之后执行</span>
// TODO Auto-generated method stub
super.onPostExecute(result);
loadContentBar.setVisibility(View.GONE);
noticeAuthor.setText(author);//设置作者
noticeTime.setText(time);//设置时间
noticeContent.setText(desc);//设置描述内容
noticeContent.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
loadContentBar.setVisibility(View.VISIBLE);
}
}</span>