%3F instead of question mark in my Retrofit URL
Retrofit won't let you put the query string in the path. Take a look at @QueryMap instead, which is designed for this.
You can achieve it by @QueryName
It allows you to attach ?
sign at the end of the Url and then the string you want after "?" Sign
You may also need to look at @QueryName(encoded = true)
if your string is already encoded.
As refs: https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/QueryMap.html
I use @QueryMap
and ImmutableMap
.
In my case, the API is: /api/static_pages/?q=A+B+C
My solution:
Step 1:
@GET("/api/static_pages")
Call<String> getStaticPagesByType(@HeaderMap HashMap<String, Object> header,
@QueryMap(encoded=true) Map<String, String> filters);
Step 2: When I want to call API:
List<String> listTypes = new ArrayList<>();
listTypes.add("A");
listTypes.add("B");
listTypes.add("C");
StringBuilder typeString = new StringBuilder();
int listSize = listType.size();
for (int i = 0; i < listSize - 1; i++) {
typeString.append(listType.get(i));
if(i != listSize-2){
typeString.append("+");
}
}
// The key is here!
ImmutableMap<String, String> types = ImmutableMap.of("q", typeString.toString());
getRetrofitClient().getStaticPagesByType(yourHeader, types)
Hope it help your work.
Happy coding.