在使用retrofit进行网络请求的时候,传递的map参数中如果存在空的value,则会报异常,那么我们就需要将map中存在空value的对象去除掉,具体操作如下:
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {String key = it.next();if (map.get(key) == null || map.get(key) == "" || map.get(key).toString() == null || map.get(key).toString().length() == 0) {map.remove(key);set = map.keySet();it = set.iterator();}
}return map;
我们在签名时候,需要对map按照key进行排序,排序方法如下:
//对map进行排序public static String sortResult(Map<String, Object> map) {if (map != null && !map.isEmpty()) {Set<String> keySet = map.keySet();List<String> list = new ArrayList<>(keySet);Collections.sort(list);StringBuilder sb = new StringBuilder();for (String key : list) {Object value = map.get(key);if (value != null){sb.append(key + "=" + URLEncoder.encode(value.toString()));}}String str = sb.toString();return str;}return null;}