【问题标题】:Jsoup select() return nothing in Android applicationJsoup select() 在 Android 应用程序中不返回任何内容
【发布时间】: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


    【解决方案1】:

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

    我想选择的课程不在此处,因为我正在获取网页的移动版本。 Android App 似乎使用了移动用户代理,这很正常,但没有在任何地方说。

    反正我知道现在在想什么。

    【讨论】:

    • 我简直要疯了!哈哈哈!和我一样的问题,谢谢大佬!!
    猜你喜欢
    • 2018-01-25
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    相关资源
    最近更新 更多