在Android WebView中只加载一个div

时间:2016-01-22 16:22:32

标签: javascript android webview

我想在WebView中仅加载一个divresults的所有网站。

我尝试过添加像这样的JavaScriptInterface:

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

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

在我的WebViewClient中:

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

但它根本不起作用,加载整个网页而不是我想要的div

1 个答案:

答案 0 :(得分:0)

1)如果要从网站URL加载部分内容,请扩展WebViewClient Link

2)使用JSOUP库来废弃网页。从网址解析中获取结果只需div id (结果)

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

并将这些内容传递到Android中的webview。

WebView webview = (WebView)this.findViewById(R.id.webview);
 webview.getSettings().setJavaScriptEnabled(true);
 webview.loadDataWithBaseURL("", content, "text/html", "UTF-8", "");