0

I want to load into the WebView only one div with id results of all the website.

I have tried adding a JavaScriptInterface like this:

        webView.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(this, "android");

        @JavascriptInterface
        public void onData(String value) {
              webView.loadData(value, "text/html", null);
        }

And inside my WebViewClient:

        @Override
        public void onPageFinished(WebView view, String url) {
            webView.loadUrl("javascript:android.onData(document.getElementById('results'));");
        }

But it simply doesn't works, the whole webpage is loaded instead of the div I want.

Grender
  • 1,589
  • 2
  • 17
  • 44

1 Answers1

0

1) If you want to load the part of the content from the website URL then extend the WebViewClient Link

2) Use the JSOUP library to scrap the web page. Get the result from URL parse it take only the div with id (result)

Document document = Jsoup.connect(LINK_TO_WEBSITE).get();
String content = document.getElementById("pnlResults").outerHtml();

and pass those content to the webview in Android.

WebView webview = (WebView)this.findViewById(R.id.webview);
 webview.getSettings().setJavaScriptEnabled(true);
 webview.loadDataWithBaseURL("", content, "text/html", "UTF-8", "");
Community
  • 1
  • 1
Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
  • It's not working, I want to get the html code of the div I want to show, not hiding the other elements, – Grender Jan 22 '16 at 17:57
  • Okay, I've tried it, but it returns me an empty div without the content: `
    `
    – Grender Jan 22 '16 at 18:19
  • Oh I get the problem, this is the url: http://dle.rae.es/?w=hola (the id of the div is "resultados"), but when it finishes loading the url changes and then fills that div. Any idea? – Grender Jan 22 '16 at 18:27
  • Can you use document.getElementsByClass("pnlResults").outerHtml(); ? Note : your sintax not is HTML, is custom object parser! –  Mar 31 '16 at 09:24
  • I suggested use the JSOUP, because from whole page He want to load specific div code. If you want to syntax fromHtml() its not useful in this situation – Maheshwar Ligade Mar 31 '16 at 09:37