被調接口的URL路徑:
//被調接口url String apiUrl = "http://api.open.xxxxxx.com/implatform/interview/send?access_token=551c619ef13c45debe92a64880f5e1cdlzJv";
將下面的key和value放到一個map中:
phonetype:1 phone:15666888553 name:張三
將map中的key和value拼裝成"&phonetype=1&phone=15666888553&name=張三"這種形式:
public static String getUrlParamsByMap(Map<String, Object> map) { if (map == null) { return ""; } StringBuffer sb = new StringBuffer(); for (Map.Entry<String, Object> entry : map.entrySet()) { sb.append(entry.getKey() + "=" + entry.getValue()); sb.append("&"); } String s = sb.toString(); if (s.endsWith("&")) { s = s.substring(0, s.length() - 1); //s = org.apache.commons.lang.StringUtils.substringBeforeLast(s, "&"); } return s; }
將上面的被調接口URL和拼裝好的查詢參數組裝起來:
//合並兩部分url public static String urlCombine(String path1, String path2){ if(CommonUtil.isNullOrEmpty(path1)) throw new NullArgumentException("path1"); if (CommonUtil.isNullOrEmpty(path2)) path2 = ""; path1 = CommonUtil.trimEnd(path1, "?"); path1 = CommonUtil.trimEnd(path1, "&"); path2 = CommonUtil.trimStart(path2,"?"); path2 = CommonUtil.trimStart(path2,"&"); if (path1.indexOf("?")>-1){ return path1+"&"+path2; } else{ return path1+"?"+path2; } }
組裝后就像下面這樣(只是舉例):
http://api.open.xxxxxx.com/implatform/interview/send?access_token=551c619ef13c45debe92a64880f5e1cdlzJv&phonetype=1&phone=15666888553&name=張三
如果覺得本文對您有幫助,不妨掃描下方微信二維碼打賞點,您的鼓勵是我前進最大的動力: