-1

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

<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请求的等价物是什么?

4

1 回答 1

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
}

}

于 2012-06-10T19:50:21.300 回答