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

发布于 2024-12-20 10:48:05 字数 1794 浏览 2 评论 0原文

我正在为我的董事会社区制作一个 Android 应用程序。论坛提供商为我提供一般类别的 RSS 提要,但不生成主题提要。因此,我从这些提要中检索主题 URL,并希望使用 Jsoup 解析 HTML 并将其提供给 WebView。

它工作得很好,除了 select() 函数不返回任何内容。

“HTML RETREIVED”日志给了我:主题标题

h1 标签位于用于测试目的的代码中:它在 WebView 和解析的网页标题上也显示良好。

我还将日志行放在 select() 行之后。它也没有返回任何内容。

我已经尝试在一个纯Java项目中仅使用Jsoup进行解析,并且进展顺利。 所以我推测 Android 出了问题。

PS:清单中的互联网权限处于活动状态。

我错过了什么吗?

这是代码:

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");
        }

I'm making an Android app for my board community. The board provider gives me RSS feeds from general categories but don't generate feeds from topics. So I retreive topics URLs from these feeds and want to parse HTML with Jsoup and give it to a WebView.

It works nice except with the select() function which returns nothing.

The "HTML RETREIVED" log gives me : <html><head><title>The topic title</title></head><body></body></html>

h1 tags are in the code on test purpose : it displays well on WebView and the title of the parsed webpage too.

I also putted the log line right after the select() line. It returns nothing too.

I've tried in a pure Java project to parse with Jsoup only and it goes well.
So I assumed something's wrong with Android.

PS : Internet permission is active in the manifest.

Did I miss something ?

Here is the code :

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");
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-12-27 10:48:05

好吧,我发现了一些有趣的事情。

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

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

Ok I've found out something interesting.

The class I wanted to select was not here because I'm getting the mobile version of the webpage. It appears Android App use a mobile user-agent, which is quite normal but not said anywhere.

Anyway I know what thinking about now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文