casper_sak_life
casper_sak_life

Reputation: 11

How to receive data from android to php web server

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

}

With code above, I'm sending values to the server, but how can server get the values sent from the device, and add it to table.

Upvotes: 1

Views: 597

Answers (1)

ernestinis
ernestinis

Reputation: 11

Instead of StringBuilder use code like this:

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

complete example: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

Upvotes: 1

Related Questions