Java脚本执行启动chrome而不是Web视图

时间:2018-09-07 06:36:18

标签: javascript android html webview

我在“ htmlString”中得到一个表单,将它包装在html,script标签中并加载到Web视图中后,我将自动重定向到需要在Web视图中执行的chrome。

这是一个快速付款表格,我需要在我的android应用程序的webview中显示付款流程。

String htmlResponse = "<HTML><BODY>"+htmlString+"</BODY> " +
                " <script> " +
                " document.getElementById('payfast-pay-form').submit(); " +
                " </script> " +
                " </HTML>";
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadData(htmlResponse , "text/html", "UTF-8");

表格数据

<form id="payfast-pay-form" action="https://sandbox.payfast.co.za/eng/process" method="post"><input type="hidden" name="merchant_id" value="243627242"><input type="hidden" name="merchant_key" value="4343343535535"><input type="hidden" name="return_url" value="https://dev.xyz.com/app/bookings/234?complete=1"><input type="hidden" name="cancel_url" value="https://dev.xyz.com/bookings/234?complete=0"><input type="hidden" name="notify_url" value="https://dev-v1-0.xyz.com/itn/settlement"><input type="hidden" name="name_first" value="Agent testing"><input type="hidden" name="name_last" value="Saloni"><input type="hidden" name="email_address" value="saloni@gmail.com"><input type="hidden" name="m_payment_id" value="234"><input type="hidden" name="amount" value="10500"><input type="hidden" name="item_name" value="We Then"><input type="hidden" name="item_description" value="xyz Booking"><input type="hidden" name="custom_int1" value=""><input type="hidden" name="custom_int2" value=""><input type="hidden" name="custom_int3" value=""><input type="hidden" name="custom_int4" value=""><input type="hidden" name="custom_int5" value=""><input type="hidden" name="custom_str1" value="settlement-amount"><input type="hidden" name="custom_str2" value=""><input type="hidden" name="custom_str3" value=""><input type="hidden" name="custom_str4" value=""><input type="hidden" name="custom_str5" value=""><input type="hidden" name="payment_method" value=""><input type="hidden" name="signature" value="afwidufuwfiurgfiwgfugfiwfw83"></form>

1 个答案:

答案 0 :(得分:1)

我的代码中缺少Web视图客户端,这就是为什么我被重定向到Chrome浏览器。

WebViewClient webViewClient = new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showProgressDialogForAgent();
            }

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

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                Log.e("shouldUrlLoading: ", request.getUrl().getPath());
                if (request.getUrl().getPath().equals("/app/bookings/"+id))
                switch (request.getUrl().getQueryParameter("complete")) {
                    case "1":
                        onPaymentSuccess();
                        break;
                    case "0":
                        onPaymentFailure();
                        break;
                }
                return super.shouldOverrideUrlLoading(view, request);
            }
        };
        webView.setWebViewClient(webViewClient);