2012-06-10 70 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)時都失敗; –