5

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.

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i have five values in spinner with diffrent name like a,b,c,d,e,f i want pass integer values of this string values..how to do it?<string-array name="planets_array"> <item value="1">a</item> <item value="2">b</item> <item value="3">c</item> <item value="4">d</item> <item value="5">e</item> <item value="6">f</item> </string-array>
@AdityaVyas, ask a new question in a new post.
Its just matter of getting selected value from spinner. Then you can use Integer.toString() method to convert Integer to String. @DarylBennett is right your question is not related to this thread.
2

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.

Comments

1

Apologies if I am stating the obvious and/or missing the point but the natural solution would appear to be to convert ints to Strings and then convert back at the server end. The more complete solution would be to use a different presentation (such as XML) to encode the data.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.