I want to send a couple of values to a web server from my Android Client using this NameValuePair
method:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http:/xxxxxxx");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
String amount = paymentAmount.getText().toString();
String email = inputEmail.getText().toString();
nameValuePairs.add(new BasicNameValuePair("donationAmount", amount));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("paymentMethod", "5"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Unfortunately NameValuePair
is only able to send String, I need to send Integer values as well.
Hi hectichavana If you want to send integer values using name value pair you can try like this
nameValuePairs.add(new BasicNameValuePair("gender",Integer.toString(1)));
where gender stands for key and 1 will become value of this key. Hope this help.
I don't think that the other end of your post request care of your value format the client using and take it all as string. So IMO that's why NameValuePair only take String. If your data is in numeric format you can always convert it back to String and pair it using NameValuePair
new BasicNameValuePair("integer", new Integer().toString(value));
is one example that I always use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With