post方法名及參數為:(具體方法可參考https://www.cnblogs.com/mufengforward/p/10510337.html)
public static String doPost(String httpUrl, String param) { ... }
如果方法參數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"} }
