How to send ArrayList<Integer> as UrlEncodedFormEntity in HTTPPOST request?

1.5k views Asked by At

Hi I'm trying to send an ArrayList as a parameter for a POST request from my Android app to a server. So far I have this code:

HttpResponse response;

        List<NameValuePair> postParams = new ArrayList<NameValuePair>(2);
        postParams.add(new BasicNameValuePair("post[text]", text));
        postParams.add(new BasicNameValuePair("post[common_comments]", String.valueOf(commonComments));
        postParams.add(new BasicNameValuePair("post[wall_ids]", wallIds);

        UrlEncodedFormEntity encodedParams;
        try {
            encodedParams = new UrlEncodedFormEntity(postParams);
            post.setEntity(encodedParams);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

but BasicNameValuePair only receives String as value. Is there any way I can send an ArrayList as a value for post[wall_ids]?

Thanks in advance.

1

There are 1 answers

0
Eloy Fernández Franco On

Bueno días Carla.
I don´t know if you had resolve this (two months is a lot of time)... but maybe it can help you:

 for(String wallyID: wallyIDs)
      postParams.add(new BasicNameValuePair("extraFields[wallyIDs][]", wallyID));

It would also be good to use the "utf 8" format, if you want to use latin characters (this will make to forget on the server), something like:

  HttpClient httpclient = new DefaultHttpClient(); 
  HttpPost httppost = new HttpPost(url);
  httppost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));

I have a question about the List constructor... why do you use 2 as argument for it??

PS: I would like to make a coment and not an answer, because I don´t know if it could work as you want.