Dynamically add query parameters without value to Retrofit request

穿精又带淫゛_ 提交于 2021-01-02 05:00:06

问题


I have request that set list of services that are turned on for user.

Request has following format:

https://myserver.com/setservices?param1=val1&param2=val2&service[10]&service[1000]&service[10000]

List of service parameters ("service[10]&service[1000]&service[10000]") is created dynamically and each parameter doesn't have value. Is it possible to achive this using Retrofit?


回答1:


From the retrofit documentation:

For complex query parameter combinations a Map can be used.

@GET("/group/{id}/users")
List<User> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options);

I guess this will make what you want to achieve.




回答2:


I found workaround how to do this.

@GET("/setservices{services_query}")
ServicesSetResponse setServices(@EncodedPath("services_query") String servicesQuery);

And then:

getService().setServices("?param1=val1&param2=val2" + "&services[10]&services[100000]&services[1000000]")


来源:https://stackoverflow.com/questions/26109103/dynamically-add-query-parameters-without-value-to-retrofit-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!