Reputation: 41
How to send GET request with query params like this? key1=["S","I","D"]&key2=[["A","X",0],["X","Y","Z"]]
Upvotes: 2
Views: 1980
Reputation: 867
Since retrofit doesn't recognize your custom array type, you have to consider parameters type String
, then build your parameter like "["S","I","D"]"
before pass to retrofit method.
For example create your parameter:
String parameter = "["
int arraySize = array.size()
for(int i = 0; i < arraySize; i++) {
parameter += "\"" + array.get(i) + "\"";
if(i != arraySize - 1) {//Append ',' except last element
parameter += ",";
}
}
parameter += "]";
Output for array with 'S','I' and 'D' elements is ["S","I","D"]
String.
Now you can pass it as retrofit parameter.
Upvotes: 0
Reputation: 1309
According to retrofit java documents, you can do like below.
Values are converted to strings using Retrofit.stringConverter(Type, Annotation[]) (or Object.toString(), if no matching string converter is installed) and then URL encoded. null values are ignored. Passing a List or array will result in a query parameter for each non-null item.
Array/Varargs Example:
@GET("/friends")
Call<ResponseBody> friends(@Query("group") String... groups);
Calling with foo.friends("coworker", "bowling") yields /friends??> group=coworker&group=bowling.
So you can do something like this
@GET("/something")
Call<ResponseBody> getSomething(@Query("key1") String[] key1,
Query("key2") String[] key2 );
foo.getSomething(key1, key2)
Update - above is the standard way to query parameters with multiple values
In order to send array as string, you can do like below
Parameter names and values are URL encoded by default. Specify encoded=true to change this behavior.
@GET("/something")
Call<ResponseBody> getSomething(@Query(value="key1", encoded=true) String key1);
Calling with foo.getSomething("['S','I','D']")) yields /something?key1=['S','I','D'].
Upvotes: 1