您的位置:首页 > 编程语言 > Java开发

java后台调用接口并且传递相关参数

2017-09-27 14:08 417 查看
已知某个接口,并且接口提供传递参数,调用接口的数据到后台解决方法:把接口以及参数用字符串拼接为一个参数,并调用代码如下: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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 接口 传参