我如何在Android上发送此类POST请求?

时间:2013-02-08 08:43:26

标签: android http post

我有这个格式页

<html><head><script>
function sendForm(form)
{
      form.value.value = "{ \"y\": " + form.example2.value + " }"
      form.submit();
}
</script></head>
<body>
<form action="https://....WEBSITE..." method="post">
<input type="hidden" name="value">
number:<input type="text" name="example2">
<input type="button" value="Submit" onClick="sendForm(this.form);">
</form>
  </body>
</html>

我想知道,如何将它转换为android中的post对象? 我希望从我的Android手机发送完全相同的数据 假设我有一个变量,我想发送我的整数,字符串代表我的网址。

2 个答案:

答案 0 :(得分:0)

使用StringEntity课程。您可以使用HttpPost

将StringEntity对象设置为setEntity()

答案 1 :(得分:0)

试试这个

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("value", value));
params.add(new BasicNameValuePair("example2", example2_value));

try {
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);
    HttpClient httpClientpost = new DefaultHttpClient(httpParameters);

    HttpPost post = new HttpPost("https://....WEBSITE...");

    UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
    post.setEntity(ent);


    HttpResponse responsePOST = httpClientpost.execute(post);
    HttpEntity resEntity = responsePOST.getEntity();
    String getresponse = EntityUtils.toString(resEntity); //Response from the server
} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e) {
    e.printStackTrace();
}