Object bean为实体对象.
public static String getBody(Object bean) {
Class classi= bean.getClass();
Field[] field= classi.getDeclaredFields();
StringBuilder stb= new StringBuilder();
for (int i= 0; i< field.length; i++) {
Field f= field[i];
f.setAccessible(true);
String key= f.getName();
Object obj= null;
try {
obj= f.get(bean);
Map map= new HashMap<>();
map.put(key, obj+ "");
Map map1= sortMapByKey(map);
for (Map.Entry entry : map1.entrySet()) {
stb.append(entry.getKey() + "=" + entry.getValue() + "&");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
Logy.d(stb.toString());
return stb.toString();
}
/**
* 使用 Map按key进行排序
*
* @parammap
* @return
*/
public static Map sortMapByKey(Map map) {
if (map== null || map.isEmpty()) {
return null;
}
Map sortMap= new TreeMap(
new MapKeyComparator());
sortMap.putAll(map);
return sortMap;
}