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
//you can add all the parameters your php needs in the BasicNameValuePair.
//The first parameter refers to the name in the php field for example
// $id=$_POST['id']; the second parameter is the value.
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
}}
上面的代碼將發送一個這樣的數組: [id=12345, stringdata=AndDev is Cool!]
如果你想有一個bidimentional數組,你應該這樣做
Bundle b= new Bundle();
b.putString("id", "12345");
b.putString("stringdata", "Android is Cool");
nameValuePairs.add(new BasicNameValuePair("info", b.toString()));
這將創建一個包含數組的數組:
[info=Bundle[{id=12345, stringdata=Android is Cool}]]
我希望這是你想。
我通常這樣做http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient – jsaye 2012-02-02 18:12:24
@jsaye:該資格作爲答案,把代碼,一些解釋鏈接下來得到一些代表 – konsolenfreddy 2012-02-03 06:45:31
@ konsolenfreddy:謝謝你的建議,我真的很新,有時我不知道該怎麼辦 – jsaye 2012-02-03 08:52:05