Jsoup select()在Android应用程序中没有返回任何内容

时间:2011-12-09 00:44:10

标签: android html-parsing jsoup

我正在为我的董事会社区制作一款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");
        }

1 个答案:

答案 0 :(得分:3)

好的,我发现了一些有趣的东西。

我想要选择的课程不在这里,因为我正在获取该网页的移动版本。看起来Android App使用移动用户代理,这很正常,但在任何地方都没有说。

无论如何,我知道现在在想什么。