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

来源:互联网 发布:商城html源码下载 编辑:程序博客网 时间:2024/05/18 03:42

已知某个接口,并且接口提供传递参数,调用接口的数据到后台

解决方法:把接口以及参数用字符串拼接为一个参数,并调用

代码如下:

 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;    }



原创粉丝点击