我在这里有一个代码,我想从android的webview中检索一个元素,但似乎无法找出问题所在。可能是一些愚蠢的错误,但我看不出原因和错误所在。谢谢。

 private void retrieveContentWebView(String address){

    Log.d("Web Download"," Inside ");
    Log.d("Web Download ","address : " + address);

    WebView webView;

    webView = new WebView(context);
    webView.loadUrl(address);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(this, "myjava");


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.getSettings().setDomStorageEnabled(true);

    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.d("Web Download"," Inside Page Started");

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            Log.d("Web Download"," Inside Page Finished");

                view.loadUrl("javascript: {" +
                        "var titleTexts = document.getElementsByTagName('title')[0]" +
                        "myjava.onData(titleTexts.text);}");

        }
    });

}

//This method will be registered as a JavaScript interface
@JavascriptInterface
@SuppressWarnings("unused")
public void onData(final String value){

    Log.d("Web Download"," Inside Handler");

    final String getValue = value;

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Do something after 5s = 5000ms
            Log.d("Web Download HTML :",getValue);
        }
    }, 5000);


}

最佳答案

尝试将其添加到您的WebView中:

webView.setWebChromeClient(new MyCustomChromeClient(this));


为自己创建MyCustomChromeClient。

10-07 19:02