0

我有这个格式页面

<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 手机发送完全相同的数据,假设我有一个变量,其中包含我希望发送的整数和代表我的 url 的字符串。

4

2 回答 2

0

使用StringEntity类。您可以将 StringEntity 对象设置为您的HttpPostusingsetEntity()

于 2013-02-08T08:45:53.433 回答
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();
}
于 2013-02-08T08:54:47.803 回答