简体   繁体   English

JSoup为Webview解析HTML

[英]JSoup Parse HTML for Webview

I need to display a part of a page in Android Studio's Webview, the section containing the PDFs. 我需要在Android Studio的Webview中显示页面的一部分,该部分包含PDF。 This is the website I need https://www.limerick.ie/council/weekly-planning-lists and the part I want to show is this http://i.imgur.com/S9Pwjte.png?1 When I try to run my code, the Webview doesn't display anything and comes up blank. 这是我需要的网站https://www.limerick.ie/council/weekly-planning-lists ,我要显示的部分是http://i.imgur.com/S9Pwjte.png?1当我尝试时要运行我的代码,Webview不会显示任何内容,而是空白。

Here is my code 这是我的代码

package com.example.john_000.jsouptest;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;

public class MainActivity extends Activity {
 public class HtmlParserActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView cardapio = (WebView) findViewById(R.id.webView);
        cardapio.getSettings().setJavaScriptEnabled(true);
        String data = "";
        Document doc = null;
        try {
            doc = Jsoup.connect("https://www.limerick.ie/council/weekly-planning-lists").get();
            Elements elements = doc.getElementsByClass("block-inner clearfix");
            for (Element element : elements) {
                data += element.outerHtml();
                data += "<br/>";
            }
            cardapio.loadData(data, "text/html", "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 }
}

If anybody knows how to parse this HTML so that I only show the required table your help would be greatly appreciated. 如果有人知道如何解析此HTML,以便我仅显示所需的表,将非常感谢您的帮助。

Replace your try-catch block with this one: 将此替换为try-catch块:

try {
    doc = Jsoup.connect("https://www.limerick.ie/council/weekly-planning-lists").get();
    Elements elements = doc.select("div.block-inner.clearfix");
    for (Element element : elements) {
        if (!element.select("tbody").isEmpty()) {
            data = element.outerHtml() + "<br/>";
            break;
        }
    }
    cardapio.loadData(data, "text/html", "UTF-8");
} catch (IOException e) {
    e.printStackTrace();
}

This is not really specific to Android (don't have my android device handy), but this works on Java: 这并不是真正针对Android的(不需要我的android设备),但这在Java上有效:

String url = "https://www.limerick.ie/council/weekly-planning-lists";

Document document = Jsoup.connect(url).get();
Element div = document.select("table.sticky-enabled").first();

String text = div.outerHtml();
System.out.println(text);

And it produces the following output: 并产生以下输出:

<table class="sticky-enabled"> 
 <thead>
  <tr>
   <th>Attachment</th>
   <th>Size</th> 
  </tr>
 </thead> 
 <tbody> 
  <tr class="odd">
   <td><span class="file"><img class="file-icon" alt="PDF icon" title="application/pdf" src="/modules/file/icons/application-pdf.png"> <a href="https://www.limerick.ie/sites/default/files/260216_applications_refused.pdf" type="application/pdf; length=6526" title="260216_applications_refused.pdf">26/02/16 Applications Refused</a></span></td>
   <td>6.37 KB</td> 
  </tr> 
  <tr class="even">
   <td><span class="file"><img class="file-icon" alt="PDF icon" title="application/pdf" src="/modules/file/icons/application-pdf.png"> <a href="https://www.limerick.ie/sites/default/files/260216_applications_granted.pdf" type="application/pdf; length=20585" title="260216_applications_granted.pdf">26/02/16 Applications Granted</a></span></td>
   <td>20.1 KB</td> 
[...]

So in your code, you can replace 因此,在您的代码中,您可以替换

Elements elements = doc.getElementsByClass("block-inner clearfix");
for (Element element : elements) {
    data += element.outerHtml();
    data += "<br/>";
}

With

data = doc.select("table.sticky-enabled").first().outerHtml();

Which would get you the complete table. 这将为您提供完整的表格。

And your data String will contain the complete HTML of the table, which you can then load into the WebView as before. 并且您的data字符串将包含表的完整HTML,然后您可以像以前一样将其加载到WebView中。 Note that if you load raw HTML into a WebView like this, it will not have any formatting or styling, since the stylesheets (CSS) are not loaded. 请注意,如果像这样将原始HTML加载到WebView中,它将没有任何格式或样式,因为未加载样式表(CSS)。

If it doesn't work: 如果不起作用:

  • Make sure your WebView is visible in your layout. 确保您的WebView在布局中可见。

  • Make sure you've added the "Internet" permission to your AndroidManifest.xml. 确保已将“ Internet”权限添加到AndroidManifest.xml。

  • Look at the LogCat (see here ), and see if you there are any exceptions, especially NetworkOnMainThreadException (Which you're probably be getting, see here .) 查看LogCat(请参阅此处 ),看看是否有任何异常,尤其是NetworkOnMainThreadException (您可能会得到的例外,请参见此处)

Let me know if it works, and if it doesn't, I'll try on an Android device and see. 让我知道它是否有效,如果无效,我将在Android设备上尝试看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM