已知某個接口,並且接口提供傳遞參數,調用接口的數據到後臺
解決方法:把接口以及參數用字符串拼接爲一個參數,並調用
代碼如下:
public Map<String, Object> TestController(HttpServletRequest request, HttpServletResponse response) {
//接口的拼接,作爲URL傳遞
String u1 =
"http://120.195.27.61:18090/AndroidGreen/BusService.asmx/GetBusRealInfoById?username=admin&passWord=123456789abc123456789abc";
String u2 = "&routeId=";
String roadId = request.getParameter("roadId");
String u3 = roadId;
String u4 = "&dir=0&tag=";
String url = u1 + u2 + u3 + u4;
List<Map<String, Object>> Testlist = new ArrayList<Map<String, Object>>();
Map<String, Object> retMap = new HashMap<String, Object>();
try {
//時間格式轉換
List<SoapEntity> SoapEntityList = new ArrayList<SoapEntity>();
SoapEntityList = busLine(url);
//調用方法在下面
for (SoapEntity d : SoapEntityList) {
Map<String, Object> businfo = new HashMap<String, Object>();
businfo.put("latitude", d.getLatitude());
businfo.put("longitude", d.getLongitude());
busList.add(businfo);
}
retMap.put("Testlist", Testlist);
return MessageUtil.buildResponseMap(true, retMap);
} catch (IOException e) {
logger.error(e.getMessage(), e);
return MessageUtil.buildResponseMap(false, MessageUtil.buildSystemErrorMessage(e), retMap);
}
}
處理busLine(url)的方法,去除格式,得到最後接口需要的格式
public static List<SoapEntity> busLine(String url) throws IOException {
List<SoapEntity> busline = new ArrayList<SoapEntity>();
Document result = Jsoup.connect(url).get();
String value = result.toString().replace("\n", "").replace("\n", "").replace("\n", "")
.replace("<string xmlns=\"http://tempuri.org/\">", "").replace("</string>", "")
.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
value = value.substring(2, value.length());
busline = JSON.parseArray(value, SoapEntity.class);
return busline;
}