我正在尝试在我的Android应用程序中实现VPOS。我有C#实现。我试图根据Android进行更改。在C#代码中;
NameValueCollection parameters = new NameValueCollection();
parameters.Add("mode", strmode); //just one of parameter.
RedirectAndPOST(this, "url", parameters);
public static void RedirectAndPOST(Page page, string destinationUrl, NameValueCollection data)
{
string strForm = PreparePOSTForm(destinationUrl, data);
page.Controls.Add(new LiteralControl(strForm));
}
private static String PreparePOSTForm(string url, NameValueCollection data)
{
string formID = "PostForm";
StringBuilder strForm = new StringBuilder();
strForm.AppendFormat("<form id=\"{0}\" name=\"{0}\" action=\"{1}\" method=\"POST\">", formID, url);
foreach (string key in data)
{
strForm.AppendFormat("<input type=\"hidden\" name=\"{0}\" value=\"{1}\">", key, data[key]);
}
strForm.Append("</form>");
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language=\"javascript\">");
strScript.AppendFormat("document.{0}.submit();", formID);
strScript.Append("</script>");
return strForm.ToString() + strScript.ToString();
}
在我的Java代码中,首先,我创建了NameValuePair
,然后,我为构造preparePOSTMethod
创建了HTML
方法。之后,我发送HTTP POST Request
。
ArrayList<NameValuePair> parameter = new ArrayList<NameValuePair>();
parameter.add(new BasicNameValuePair("mode", strmode));
parameter.add(new BasicNameValuePair("cardnumber", cardNumber));
parameter.add(new BasicNameValuePair("cardexpiredatemonth", cardExpireDateMonth));
parameter.add(new BasicNameValuePair("cardexpiredateyear", cardExpireDateYear));
parameter.add(new BasicNameValuePair("cardcvv2", CardCvv2));
post = preparePOSTForm("url", parameter);
private static String preparePOSTForm(String url, ArrayList<NameValuePair> data)
{
String formID = "PostForm";
StringBuilder strForm = new StringBuilder();
strForm.append(String.format("<form id=\"{0}\" name=\"{0}\" action=\"{1}\" method=\"POST\">", formID, url));
for (int i=0; i<data.size(); i++) {
strForm.append(String.format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\">", data.get(i).getName(), data.get(i).getValue()));
}
strForm.append("</form>");
StringBuilder strScript = new StringBuilder();
strScript.append("<script language=\"javascript\">");
strScript.append(String.format("document.{0}.submit();", formID));
strScript.append("</script>");
param = strForm.toString() + strScript.toString();
System.out.println("result: "+param);
return strForm.toString() + strScript.toString();
}
但是,我无法构建HTML。
Result: <form id="{0}" name="{0}" action="{1}" method="POST"><input type="hidden" name="{0}" value="{1}"><input type="hidden" name="{0}" value="{1}"><input type="hidden" name="{0}" value="{1}"><input type="hidden" name="{0}" value="{1}"><input type="hidden" name="{0}" value="{1}"><input type="hidden" name="{0}" value="{1}"></form><script language="javascript">document.{0}.submit();</script>
如何使用Name-Value Pair和StringBuilder或其他东西构建HTML
?
答案 0 :(得分:1)
strForm.append(String.format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\">"
, data.get(i).getName(), data.get(i).getValue()));
如您所见,未填写参数。您只需更改格式字符串。
使用%s代替{0}和{1}。