android发布数据,html表单

时间:2012-06-10 19:42:36

标签: android post

我想将HTTP POST请求发送到HTML源代码中提到的具有相同参数的URL。 表格代码:

<form enctype="multipart/form-data" method="post" action="/add/">
   <div style="display:none"><input type="hidden" name="csrfmiddlewaretoken" value="3e7651db3a8925c8079990f0cb13d8f7"></div>   
   <table>
       <tbody><tr><th><label for="id_key">Key:</label></th><td><input id="id_key" type="text" name="key" maxlength="13"></td></tr>
<tr><th><label for="id_val">Val:</label></th><td><input id="id_val" type="text" name="val" maxlength="50"></td></tr>
    </tbody></table>
    <input type="submit" value="Submit" id="Save">
</form>

在android中发送POST请求相当于什么?

1 个答案:

答案 0 :(得分:1)

我假设你想调用一个从原生android代码执行HTTP POST的函数。

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

}