2013-02-17 116 views
1
StringBuilder sb = new StringBuilder(); 
sb.append("couponID=1"); 
sb.append("CouponName=Wendy"); 
sb.append("couponID=2"); 
sb.append("CouponName=Wen"); 
sb.append("couponID=3"); 
sb.append("CouponName=Danny"); 
URL Url; 
String url="http://example/upload"; 
try { 
Url = new URL(url); 
HttpURLConnection conn = (HttpURLConnection) Url.openConnection(); 
conn.setDoOutput(true); 
conn.setRequestMethod("POST"); 
OutputStreamWriter wr = new OutputStreamWriter(conn 
    .getOutputStream()); 

     wr.write(sb.toString()); 
wr.flush(); 
wr.close(); 
} catch (Exception e) { 
     System.out.println(e.toString()); 

} 

有了上面的代碼,我送值到服務器,但如何能服務器獲取值從設備發送,並將其添加到表中。如何從Android的接收數據到PHP的Web服務器

+1

在此一對夫婦的意見。你需要將你正在編寫的變量與'&'分開。您也應該小心使用相同的變量名兩次('CouponName'),因爲PHP無法處理它。考慮使用「Coupon3 = Danny」代替。 – 2013-02-17 06:39:23

回答

1

代替StringBuilder使用這樣的代碼:

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)); 

完整的例子: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

+0

好吧,我試過這個方法。如何發送多個ID和字符串,例如:data.nameValuePairs.add(new BasicNameValuePair(「id」,「12345」)); (新的BasicNameValuePair(「stringdata」,「AndDev is Cool!」)); nameValuePairs.add(new BasicNameValuePair(「id」,「12348」)); nameValuePairs.add(new BasicNameValuePair(「stringdata」,「wassup」));我會使用JSON來處理複雜的數據,請參考 – 2013-02-17 07:05:41

+0

。 – ernestinis 2013-02-17 14:11:59

相關問題