hectichavana
hectichavana

Reputation: 1446

Sending Integer to HTTP Server using NameValuePair

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.

Upvotes: 5

Views: 14095

Answers (3)

Roshan Jha
Roshan Jha

Reputation: 2091

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.

Upvotes: 13

Raymond Lagonda
Raymond Lagonda

Reputation: 1253

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.

Upvotes: 2

airewyre
airewyre

Reputation: 1179

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.

Upvotes: 1

Related Questions