栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Retrofit-多个具有相同名称的查询参数,其中名称是动态设置的

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Retrofit-多个具有相同名称的查询参数,其中名称是动态设置的

感谢@ILLIA
DEREVIANKO(https://github.com/square/retrofit/issues/1324)发布的链接,我设法解决了此类问题:

   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;    }}

这样我们就可以使用一个映射,其中key是唯一的参数名称,value是一个字符串列表,它是此参数的值。像这样:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号