0

I need to display a part of a page in Android Studio's Webview, the section containing the PDFs. This is the website I need https://www.limerick.ie/council/weekly-planning-lists and the part I want to show is this https://i.sstatic.net/6HfsL.png When I try to run my code, the Webview doesn't display anything and comes up blank.

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.

2 Answers 2

1

Replace your try-catch block with this one:

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(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply. Unfortunately my emulator is still showing up empty.
0

This is not really specific to Android (don't have my android device handy), but this works on 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. 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.

If it doesn't work:

  • Make sure your WebView is visible in your layout.

  • Make sure you've added the "Internet" permission to your 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.)

Let me know if it works, and if it doesn't, I'll try on an Android device and see.

7 Comments

Thanks for replying. The same problem is still persisting where my WebView shows up empty. The WebView is visible and internet permission have already been added. No NetworkOnMainThreadException in LogCat. Instead I get:
03-19 12:49:40.466 26845-26870/com.example.john_000.jsouptest W/EGL_emulation: eglSurfaceAttrib not implemented 03-19 12:49:40.466 26845-26870/com.example.john_000.jsouptest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaec38740, error=EGL_SUCCESS 03-19 12:49:40.506 26845-26845/com.example.john_000.jsouptest W/ViewRootImpl: Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=54637, eventTime=4124943, downTime=1295252, deviceId=1, source=0x301 }
@GeneralDisorder, Hmm, that doesn't seem related to Jsoup. I'll try your code on a real device and check it. In the meantime, try loading a predefined String into your WebView, ie. with something like this: cardapio.loadData("Some text", "text/html", "UTF-8");. Is your WebView still blank, or does it show something ? In that case, it's the Jsoup part in your code which is not working right. Also try to output your data / text to the LogCat, eg with System.out.println(data). Do you get anything in LogCat ?
When I put in cardapio.loadData("Some text", "text/html", "UTF-8"); My webview still comes up empty. I'm almost certain I have Jsoup setup though. I have my jsoup-1.8.3.jar file placed in libs and have "compile files('libs/jsoup-1.8.3.jar')" added to app build.gradle.
This is what is looks like with 'cardapio.loadData("Some text", "text/html", "UTF-8");' added in imgur.com/Qag3ONx
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.