gpt4 book ai didi

Javascript 无法在 WebView 中工作 - Android

转载 作者:行者123 更新时间:2023-12-03 05:21:43 24 4
gpt4 key购买 nike

我这里有一段代码,我想从 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。

关于Javascript 无法在 WebView 中工作 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41348639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com