【发布时间】:2012-01-16 09:44:37
【问题描述】:
我正在为我的董事会社区制作一个 Android 应用程序。版块提供者为我提供一般类别的 RSS 提要,但不从主题生成提要。因此,我从这些提要中检索主题 URL,并希望使用 Jsoup 解析 HTML 并将其提供给 WebView。
除了不返回任何内容的 select() 函数外,它工作得很好。
“HTML RETREIVED”日志给了我:<html><head><title>The topic title</title></head><body></body></html>
h1 标记在代码中用于测试目的:它在 WebView 和解析网页的标题上也能很好地显示。
我还将日志行放在 select() 行之后。它也不返回任何内容。
我已经尝试在纯 Java 项目中仅使用 Jsoup 进行解析,并且运行良好。 所以我认为 Android 出了点问题。
PS : 清单中的 Internet 权限处于活动状态。
我错过了什么吗?
代码如下:
String html;
Bundle param = this.getIntent().getExtras();
String url = param.getString("url");
try {
Document doc = Jsoup.connect(url).get();
doc.select(".topic .clear").remove();
String title = doc.title().toString();
html = doc.select(".username strong, .entry-content").toString();
html = "<html><head><title>"+title+"</title></head><body><h1>"+title+"</h1>"+html+"</body></html>";
WebView webview = new WebView(this);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
Log.d("LOADING",""+ progress);
}
});
webview.loadData(html, "text/html", "UTF-8");
//webview.loadUrl(url);
Log.i("HTML RETREIVED", ""+html);
} catch (IOException e) {
Log.e("ERROR", "Error while generate topic");
}
【问题讨论】:
标签: android html-parsing jsoup