从Android Webview获取POST数据

时间:2014-04-02 12:56:42

标签: android post webview payment-gateway

我在我的Android应用程序中使用支付网关。我正在使用webview加载支付页面。我已经提供了一个重定向URL到支付网关,确认付款后webview将被重定向到该网关。银行的确认(成功/失败)将回发到此URL。我可以将我的webview重定向到此URL以向客户显示事务成功。我需要获取发送到重定向URL的POST数据。如果事务成功,我需要在我的应用程序中下订单。我目前正在做的是,我正在检查重定向网址,是否是成功交易。我想知道是否有其他方法可以用来检查我的交易状态?这是我的代码,

 mWebview  = (WebView)findViewById(R.id.webView1);

                mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
                mWebview.getSettings().setAppCacheEnabled(false);
                mWebview.getSettings().setLoadWithOverviewMode(true);
                mWebview.getSettings().setUseWideViewPort(true);
                mWebview.getSettings().setBuiltInZoomControls(true);


                mWebview.setWebViewClient(new WebViewClient() {
                    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                        Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
                    }

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


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


                        String webUrl = mWebview.getUrl();


                        Log.i("RETURN URL", "RETURN URL IS "+webUrl);


                            if(url.equals("http://www.mydomain.in/trxn_complete")) //This is my method.But I think its ugly one
                       {
                              AlertDialog alertDialog = new AlertDialog.Builder(OnlinePaymentActivity.this).create();               

                                alertDialog.setMessage("Transaction successful.Press OK to continue");
                                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                                       public void onClick(DialogInterface dialog, int which) {
                                          // TODO Add your code for the button here.


                                               //Transaction success, So place order
                                               new Orderitems(OnlinePaymentActivity.this).execute();




                                       }
                                    });
                                alertDialog.show();
                        }




}

 });


         mWebview .loadUrl("http://263.134.260.173/gateway/epi/fts?ttype="+type+"&tempTxnId="+tempTxnId+"&token="+token+"&txnStage="+txnStage);  




        }

1 个答案:

答案 0 :(得分:1)

试试这个:

private static final String paymentReturnUrl="http:/yourUrl";

private class FormDataInterface {

        @JavascriptInterface
        public void processFormData(String url,String formData) {
            Log.d(DEBUG_TAG,"Url:"+url+" form data "+formData);
            if(url.equals(paymentReturnUrl)){
                HashMap<String,String> map=new HashMap<>();
                String[] values = formData.split("&");
                for(String pair :values){
                    String[] nameValue=pair.split("=");
                    if(nameValue.length==2){
                        Log.d(DEBUG_TAG,"Name:"+nameValue[0]+" value:"+nameValue[1]);
                        map.put(nameValue[0],nameValue[1]);
                    }
                }

                return;
            }
        }
    }

    private class CustomWebViewClient extends WebViewClient{
        private final String jsCode ="" + "function parseForm(form){"+
            "var values='';"+
            "for(var i=0 ; i< form.elements.length; i++){"+
            "   values+=form.elements[i].name+'='+form.elements[i].value+'&'"+
            "}"+
            "var url=form.action;"+
                "console.log('parse form fired');"+
                "window.FORMOUT.processFormData(url,values);"+
    "   }"+
        "for(var i=0 ; i< document.forms.length ; i++){"+
        "   parseForm(document.forms[i]);"+
                "};";



        private static final String DEBUG_TAG = "CustomWebClient";

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if(url.equals(paymentReturnUrl)){
                Log.d(DEBUG_TAG,"return url cancelling");
                view.stopLoading();
                return;
            }
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            Log.d(DEBUG_TAG, "Url: "+url);
            if(url.equals(paymentReturnUrl)){
                return;
            }
            view.loadUrl("javascript:(function() { " + jsCode + "})()");

            super.onPageFinished(view, url);
        }

    }

使用以下命令初始化webview:

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.addJavascriptInterface(new FormDataInterface(), "FORMOUT");
webView.setWebViewClient(new CustomWebViewClient());