如何从android接收数据到php web服务器

时间:2013-02-17 06:13:55

标签: android codeigniter

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

}

使用上面的代码,我将值发送到服务器,但服务器如何获取从设备发送的值,并将其添加到表中。

1 个答案:

答案 0 :(得分: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