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

}

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

4

1 回答 1

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

于 2013-02-17T06:58:52.827 回答