1

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.

Carla Urrea Stabile
  • 869
  • 1
  • 11
  • 35
  • Whenever I've faced this problem in the past, I've just sent it as a comma-separated `String` with the `Integer` values in it. I suspect there are more efficient ways to do it; but it works for me! – Dawood ibn Kareem Dec 01 '14 at 20:19
  • My suggestion is: convert your array to *JSON* object(JSON array for example) and send it as string – Rami Dec 01 '14 at 20:20

1 Answers1

1

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.

Eloy Fernández Franco
  • 1,350
  • 1
  • 24
  • 47