I am working with web services. I want to create a JSONObject and send the values through web service. How to do it?
String redeem = edit_messageredeem.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
String httppostURL = "http:// ...";
HttpPost httppost = new HttpPost(httppostURL);
Log.v(TAG, "postURL: " + httppost);
List<NameValuePair> data2 = new ArrayList<NameValuePair>(5);
data2.add(new BasicNameValuePair("merchant_id", "02"));
data2.add(new BasicNameValuePair("merchant_location_id", "03"));
data2.add(new BasicNameValuePair("user_id", "04"));
data2.add(new BasicNameValuePair("merchant_kiosk_id", "04"));
data2.add(new BasicNameValuePair("coupon_code", redeem));
httppost.setHeader(HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded;charset=UTF-8");
httppost.setEntity(new UrlEncodedFormEntity(data2 , "UTF-8"));
// httppost.setEntity(new UrlEncodedFormEntity(data));
// httpclient.execute(httppost);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
Log.i("TAG",""+response.getStatusLine().getStatusCode());
Toast.makeText(RedeemActivity.this, responseStr, Toast.LENGTH_LONG).show();
// you can add an if statement here and do other actions based on the response
}
edit_messageredeem.setText(""); //reset the message text field
// Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
Toast.makeText(RedeemActivity.this, "Data: " +data2,Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable t) {
Toast.makeText(RedeemActivity.this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
}