Android HttpUrlConnection send multiple parameters with same key - java

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

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

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());

It is not possible with params.put() But it is possible with params.add()
Reference : Difference between RequestParams add() and put() in AndroidAsyncHttp

Related

Firestore fields null type usage

I have a collection of groups, each group document has a map of admins. I store admins ids as keys of the map and for the values, I use the null type. So, when I want to retrieve this map, what is the java type to use, since null is not type in java, It's more of a value. I mean I can not have a variable of type null in java. So, How should my map look like :
HashMap<String, ?what-type> map = new HashMap();
What is that ?what-type ?
Even if null is a supported data-type in Cloud Firestore, don't store null as values, rather store a boolean value, for example, true. In this way, the type of the object you were looking for is Boolean. So now you can use:
HashMap<String, Boolean> map = new HashMap();
However, a more appropriate solution would be to use:
HashMap<String, Object> map = new HashMap();
As you don't always know the type of the value. So setting that as an Object might be more helpful in this case. Please note, that an object can also have the value of null, so this solution will cover all situations.

Freemarker - access value at a particular key in map

I have Map variable in my Freemarker template. How can I fetch a value at a particular key in the map, as we do in Java (map.get(<key>)).
I know how to iterate through keys and values of a map in a FTL. But I want a solution without iteration, on the lines of Java get() method of Map interface.
map[dynamicKey] or map.staticKey if the key is a string. Due to historical limitations, if the key isn't a string, map?api.get(nonStringKey).

Java QueryParam with key and value in the same parameter

I'm creating a endpoint (using Jersey annotations) that receives a list of Strings as it's parameter. The strings need to contain a key and a value.
So on the url it would look something like
?params=key1=value1&params=key2=value2&params....
Two questions:
I'm not sure if the above example would work, I assume you would have to encode the = sign between key and value. Is that correct?
Is there a better or more standard way to approach this?
Obviously the most standard way would be to have the RHS be the key and the LHS of the equal sign would be the value. The problem is I don't want to define every possible key value pair in my method signature. I want to accept a general list of keys and values and let the user pass in the pairs that are relevant to their query.
So far I've tried several google queries but haven't found an answer:
java url key and value parameter
java url parameters with key and value
list of key value pairs in url java
Storing a list of key value pairs in a url parameter, javascript. (has an answer for php but I'm using Java, would this work in Java?)
how to send an array in url request (suggests the above would work in java, if so, can the index be a string like &params[key2]=value2...?)
Yes. Jersey can handle it even = between key and value is not encoded.If query parameter is params=key1=value1&params=key2=value2 , the params list below will be [key1=value1, key2=value2]
#GET
public Response q34211549(#QueryParam("params") List<String> params) {
//parse params list and convert it into key and value
}
You can also consider to create a ParamConverter to let Jersey to do this conversion for you.
Reference
JAX-RS ParamConverter and ParamConverterProvider Example

JAVA - eclipse debug remove/change a key in map

I have a situation where I need to change(remove will also work) value corresponding to a key in a Map.
There are so many pairs in map. I don't want to copy all and create new map in change value for Map.
Is there any way I can directly change/remove value corresponding to a key.
I have tried changing complete map like :
Map m = commandParameters;
m.put("AID","");
return m;
But commandParameters is not resolved.
I tried changing that particular entry using random expressions, but could not work out.
Is there any way to do so?
**EDIT : ** commandParameters is original map.
Just do a remove on the map for a specific key.
commandParameters.remove("AID");
commandParameters.put("AID", "newvalue");
return commandParameters;

Differance between getHeaderFields v/s getRequestProperties in HttpURLConnection in android

I was going through the HttpURLConnection API doc and found two methods
getHeaderFields () Which Returns an un-modifiable map of the response-header fields and values
and
getRequestProperties () which also Returns an un-modifiable map of general request properties used by this connection.
I want to know what is difference between this two methods?
From My understanding both methods are used to store key,value pair in HTTP Header.
Correct me if I am wrong.
Thanks in advance.
getHeaderFields() returns all the fields and values while getRequestProperties(String field) returns the properties of the requested fields (not all).
From < Android API 22 Platform > javaDoc:
public Map<...> getHeaderFields ()
Added in API level 1
Returns an unmodifiable map of the response-header fields and values. The response-header field names are the key values of the map. The map values are lists of header field values associated with a particular key name.
Some implementations (notably HttpURLConnection) include a mapping for the null key; in HTTP's case, this maps to the HTTP status line and is treated as being at position 0 when indexing into the header fields.
Returns
the response-header representing generic map
And for request
public Map<...> getRequestProperties ()
Added in API level 1
Returns an unmodifiable map of general request properties used by this connection. The request property names are the key values of the map. The map values are lists of property values of the corresponding key name.
Returns
the request-property representing generic map.

Categories

Resources