Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Integer to HTTP Server using NameValuePair

Tags:

android

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.

like image 982
hectichavana Avatar asked Dec 22 '11 12:12

hectichavana


2 Answers

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.

like image 50
Roshan Jha Avatar answered Oct 02 '22 14:10

Roshan Jha


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.

like image 23
Raymond Lagonda Avatar answered Oct 02 '22 15:10

Raymond Lagonda