我正在尝试在加载webView时注入JavaScript以读取特定值.
这些是我用于webView的属性.
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setCacheMode(2);
webView.getSettings().setDomStorageEnabled(true);
webView.clearHistory();
webView.clearCache(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadWithOverviewmode(false);
webView.addJavascriptInterface(new MyJavaScriptInterface(),"HTMLOUT");
我正在我的onPageFInished()方法中注入javacript.
@Override
public void onPageFinished(final WebView view,final String url) {
webView.post(new Runnable() {
@Override
public void run() {
webView.loadUrl("javascript:window.HTMLOUT.showHTML('
下面的代码是MyJavaScriptInterface.
public class MyJavaScriptInterface{
@JavascriptInterface
public void showHTML(String html_data) {
if(html_data.contains("response_code")){
Log.e(TAG," ======> HTML Data : "+ html_data);
new MakeQueryPayment().execute();
}
}
}
从Logcat捕获的错误.
01-08 17:56:43.701 I/chromium(27026): [INFO:CONSOLE(1)] "Uncaught TypeError: window.HTMLOUT.showHTML is not a function",source: (1)
我只在三星Galaxy Tab A中遇到此问题,型号为SM-T550,Android版本为5.0.2.在我们拥有的其他设备中,它工作正常.任何人都可以帮助我解决这个问题.
提前致谢.
最佳答案
我尝试使用Galaxy Tab 4,这个代码在这个设备上运行良好.
WebView properties
WebView webView = new WebView(this);
setContentView(webView);
webView.clearHistory();
webView.clearCache(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setCacheMode(2);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadWithOverviewmode(false);
webView.addJavascriptInterface(new MyJavaScriptInterface(),"HTMLOUT");
webView.loadUrl("https://stackoverflow.com/questions/34746626/uncaught-typeerror-window-htmlout-showhtml-is-not-a-function");
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url) {
return true;
}
@Override
public void onPageStarted(WebView view,String url,Bitmap favicon) {
}
public void onPageFinished(WebView view,String url) {
view.loadUrl("javascript:window.HTMLOUT.showHTML('
JavaScript Interface
public class MyJavaScriptInterface{
@JavascriptInterface
public void showHTML(String html_data) {
Log.e(""," ======> HTML Data : "+ html_data);
}
}