WebSettings settings = webView.getSettings(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);//为了https } settings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(WebActivity.this, "web"); webView.loadUrl("file:///android_asset/demo.html");webView.setWebViewClient(new WebViewClient() {//html页面加载完传值过去这样才有效 @Override public void onPageFinished(WebView view, String url) {//当页面加载完成 super.onPageFinished(view, url); webView.loadUrl("javascript:setPlain('" + mPlain + "')");//调用html里面的function webView.loadUrl("javascript:setSignature('" + mSignature.replaceAll("\r\n", "") + "')"); webView.loadUrl("javascript:submitUrl()");//调用html里面的function 提交了表单 } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); // 接受网站证书 } }); @JavascriptInterface public void orderComplete() {//html调到activity} //html <script type="text/javascript"> function setPlain(plain){ document.getElementById("plain").value = plain; } function setSignature(signature){ document.getElementById("signature").value = signature; } function submitUrl(){ document.getElementById("form").submit(); } </script> <body> <!--<form id="form" action="http://127.0.0.1:8003/paygate/main" method="post">--> <!--<input type="hidden" id="plain" name="Plain">--> <!--<input type="hidden" id="signature" name="Signature">--> <!--</form>--> </body>