gpt4 book ai didi

android - 从 Android Webview 获取 POST 数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:53 24 4
gpt4 key购买 nike

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

 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);




}

最佳答案

试试这个:

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());

关于android - 从 Android Webview 获取 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22812233/

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