1

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

example:

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

Community
  • 1
  • 1
HaBtibym
  • 13
  • 3

3 Answers3

0

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

Smittey
  • 2,475
  • 10
  • 28
  • 35
0

Alternatively You can do this and it will work fine.

You can make a JSONArray like this

JSONArray array = new JSONArray();
array.put("value1");
array.put("value2");

//and then you can send them as parameter like this-

params.put("key", array.toString());
Navin Gupta
  • 793
  • 6
  • 20
0

It is not possible with params.put() But it is possible with params.add()

Reference : Difference between RequestParams add() and put() in AndroidAsyncHttp

Community
  • 1
  • 1
TharakaNirmana
  • 10,237
  • 8
  • 50
  • 69