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

标签 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:互联网许可在 list 中处于 Activity 状态。

我错过了什么吗?

代码如下:

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

最佳答案

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

我要选择的类(class)不在这里,因为我正在获取网页的移动版本。 Android 应用程序似乎使用了移动用户代理,这很正常但没有在任何地方说。

不管怎样,我知道现在在想什么。

关于android - Jsoup select() 在 Android 应用程序中不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8439739/

相关文章:

java - 使用 JTidy 解析链接

java - 使用 Java 6 和 Jsoup 1.7.3,如何解析同级文本不在元素内的 HTML?

java - 使用 JSoup 获取一些属性

java - JSoup 获取 div 的第一个子元素

android - Sony Xperia (E2003) 中的调试突然停止工作

java - 如果Java中有空白字段,如何发出错误消息?

PHP DOMDocument 解析 HTML

android - Phonegap 应用程序可在模拟器中运行,但不能在设备上运行

java - 迭代 Map 时出现 ConcurrentModificationException

PHP DOM : Get all text values