Post with duplicate parameter names (array)

1,034 views
Skip to first unread message

Paris Diamantopoulos

unread,
Nov 15, 2013, 6:12:14 AM11/15/13
to volley...@googlegroups.com
I need to send a POST request with duplicate parameter names, like "param=a&param=b".

Overriding the Request.getParams() does not work since Map<String, String> cannot have duplicate keys, so only one value would be sent.

I know I can rewrite the Request class to use a Map<String, String[]> or Map<String, List<String>>, but I was wandering if there is any other way that would not require altering the library.

Thanks in advance,
Paris

Ficus Kirkpatrick

unread,
Nov 16, 2013, 9:50:51 AM11/16/13
to Paris Diamantopoulos, volley...@googlegroups.com

You can override getBody() without having to modify the library.

F

--
You received this message because you are subscribed to the Google Groups "Volley Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to volley-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Maurie Danko

unread,
Apr 15, 2014, 12:26:44 AM4/15/14
to volley...@googlegroups.com, Paris Diamantopoulos
we did this in our Request overriding class

        // Volley library uses HashMap<String,String> which doesn't support duplicate keys
        @Override
        public byte[] getBody() throws AuthFailureError {
            if (postOrPutParams != null) {
                StringBuilder builder = new StringBuilder("");
                for (BasicNameValuePair nv : postOrPutParams) {
                    builder.append("&").append(nv.getName());
                    if (nv.getValue() != null) {
                        builder.append("=").append(nv.getValue());
                    }
                }
                if (builder.length() > 0) { // ! isEmpty()
                    builder.deleteCharAt(0);
                }
                return builder.toString().getBytes();
            }
            return null;

harry dessieu

unread,
May 6, 2014, 3:40:50 PM5/6/14
to volley...@googlegroups.com
It is true that Map<String, String> does not support duplicate but however that you
 could do something like this. you won't have to override getBody(), just the getParams which i'm assuming you're already doing.


   @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();;
                params.putAll(AddArrayParams());
 
                return params;
            }

////////////// you will use this method to add the array parameter


public  Map<? extends String, ? extends String> AddArrayParams() {
// TODO Auto-generated method stub
Map<String, String> params = new HashMap<String, String>();
                // figure that if its an array and the data is sent as [0],[1] then lets just send it up that way
                params.put("param[0]","a");
                         params.put("param[1]","b");
                         params.put("param[3]","c");
                        ////etc

   return params;
}

Good Luck 

Ahmed Ashour

unread,
Nov 5, 2014, 10:40:01 AM11/5/14
to volley...@googlegroups.com
Thanks harray your way work with me
Reply all
Reply to author
Forward
0 new messages