如果方法参数param是要求以json字符串的形式传递则:
1. 如果是JSONObject对象转字符串则:String result = HttpUtil.doPost(URL, json.toJsonString());
2. Map转字符串则需采用:String result = HttpUtil.doPost(URL, JSON.toJSONString(map));
注:使用map.toString() 时会出现参数解析不到的问题
因为:json.toJsonString()转换后为:{"name":"ceshi","password":"123456"}
map.toString()转换后为:{password=123456, name=ceshi}
对比可知,参数不一致;
测试方法如下:
public static void main(String[] args) { Map map = new HashMap<>(); map.put("name", "ceshi"); map.put("password", "123456"); System.out.println(map.toString()); //{password=123456, name=ceshi} System.out.println(JSON.toJSONString(map)); //{"name":"ceshi","password":"123456"} JSONObject json = new JSONObject(); json.put("name", "ceshi"); json.put("password", "123456"); System.out.println(json.toJSONString()); //{"name":"ceshi","password":"123456"} }
参考:https://www.cnblogs.com/mufengforward/p/10707484.html