2012-06-10 31 views
-1

我想发送HTTP POST请求到HTML源代码中提到的URL上,并使用相同的参数。 表单代码:android发布数据,html表格

<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请求的相同呢?

+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 
} 

}

+0

我在我的活动中尝试过这个功能,这是第二项活动,但每次尝试httpclient.execute(httppost)时都失败; –