HaBtibym HaBtibym - 3 months ago 16x
Java Question

Android HttpUrlConnection send multiple parameters with same key

How can i add multiple values with the same name in a HttpUrlConnection request.


HashMap<String, String> params = new HashMap<>();
params.put("key[]", value1)
params.put("key[]", value2)

If i try to add multiple values with the same in postman i works fine, the application will send only one values (depends on request property, URLConnection setRequestProperty vs addRequestProperty).

I want to add both values as a parameter with the same name


This is not possible with Maps or HashMaps.

Taken from Oracles documentation on Maps: http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

The put command will replace the previous value associated with the given key in the map (you can think of this like an array indexing operation for primitive types).

The Oracle Documentation for put states:

Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

Returns the previous value associated with key, or null if there was no mapping for key.

This can be found here: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#put%28K,%20V%29