Retrofit — Multiple query parameters of same name where name is set dynamically

 ̄綄美尐妖づ 提交于 2019-12-21 21:43:13

问题


I'm trying to migrate an old project to Retrofit library and this project has quite tricky API. So I have a query template like this:

@GET(value = "products/search")
Single<ProductSearchResponse> productSearch();

And I have to add some parameters here of following template:

filter[attributeId]=attributeValueId

For example:

products/search?filter[1]=10&filter[1]=11&filter[2]=20&filter[2]=21

That's how API works and I can't change it. I know that we can pass a list as a parameter, like this:

@Query("filter") List<Integer> attributeValueIds

But how can I also set parameter's name dynamically?


回答1:


You can use @QueryMap annotation like this:

public interface NewsService() {  
    @GET("/news")
    Call<List<News>> getNews(
        @QueryMap Map<String, String> options
    );
}

Map<String, String> data = new HashMap<>();
data.put("author", "Marcus");
data.put("page", String.valueOf(2));

...
newsService.getNews(data);

More details: https://futurestud.io/tutorials/retrofit-2-add-multiple-query-parameter-with-querymap




回答2:


Thanks to the link, posted by @ILLIA DEREVIANKO (https://github.com/square/retrofit/issues/1324), I've managed to solve the problem with this class:

   public class ProxyRetrofitQueryMap extends HashMap<String, Object> {
    public ProxyRetrofitQueryMap(Map<String, Object> m) {
        super(m);
    }

    @Override
    public Set<Entry<String, Object>> entrySet() {
        Set<Entry<String, Object>> originSet = super.entrySet();
        Set<Entry<String, Object>> newSet = new HashSet<>();

        for (Entry<String, Object> entry : originSet) {
            String entryKey = entry.getKey();
            if (entryKey == null) {
                throw new IllegalArgumentException("Query map contained null key.");
            }
            Object entryValue = entry.getValue();
            if (entryValue == null) {
                throw new IllegalArgumentException(
                        "Query map contained null value for key '" + entryKey + "'.");
            }
            else if(entryValue instanceof List) {
                for(Object arrayValue:(List)entryValue)  {
                    if (arrayValue != null) { // Skip null values
                        Entry<String, Object> newEntry = new AbstractMap.SimpleEntry<>(entryKey, arrayValue);
                        newSet.add(newEntry);
                    }
                }
            }
            else {
                Entry<String, Object> newEntry = new AbstractMap.SimpleEntry<>(entryKey, entryValue);
                newSet.add(newEntry);
            }
        }
        return newSet;
    }
}

With this we can just use a map, where key is a unique parameter name and value is a List of Strings, that are values for this parameter. Something like this:

ProxyRetrofitQueryMap map = new ProxyRetrofitQueryMap();

    List<String> values1 = new ArrayList<>();
    values1.add("10");
    values1.add("11");
    map.put("filter[1]", values1);

    List<String> values2 = new ArrayList<>();
    values1.add("20");
    values1.add("21");
    map.put("filter[2]", values2);


来源:https://stackoverflow.com/questions/52985124/retrofit-multiple-query-parameters-of-same-name-where-name-is-set-dynamically

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