跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

java调用webservice接口,并解析返回参数问题

目录java调用webservice接口,并解析返回参数

1. 设置传参

2. 调用对端接口

2.解析返回的参数

总结java调用webservice接口,并解析返回参数

1. 设置传参

例如以下格式:

// 确定传参格式以及赋值String reqXml = "\n" +"auth\n" +"auth\n" +""+rootTicket+"\n" +""+userAccount+"\n" +"1000\n" +""+operationCode+"\n" +"668801\n" +""+mode+"\n" +""+sendApplyParam.getCerReason()+"\n" +"9999\n" +""+sendApplyParam.getExpire()+"\n" +""+ip+"\n" +""+sendApplyParam.getApproverId()+"\n" +"999999\n" +"9999\n" +"\n";

2. 调用对端接口

//方法调用//reqXml 传入的参数信息//applyInfoUrl 对端接口的请求地址//type 对端接口的方法名,如果只是调用一个方法名可以写死String result = DocumentTrans.send(reqXml,applyInfoUrl,type);调用实现代码 //调用接口public static String send(String params,String url,String type) {log.info("===传递的参数==="+params);log.info("===请求路径==="+url);String result="";org.apache.axis.client.Service service = new org.apache.axis.client.Service();Call call = null;try {call = service.createCall();} catch (ServiceException e) {e.printStackTrace();}//10.174.242.24:7001 call.setTargetEndpointAddress(url);//3、设置参数 in0(对端的方法中的参数名) call.addParameter("in0",org.apache.axis.encoding.XMLType.XSD_STRING, //参数类型javax.xml.rpc.ParameterMode.IN);// 接口的参数// 设置返回类型call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);try {result = (String) call.invoke(QName.valueOf(type), new Object[]{params});} catch (Exception e) {e.printStackTrace();throw new NrmsYnException("接口异常");}return result;}

2.解析返回的参数

转为List> //2.1 先调用解析public static Document DocumentHelperreadStringXml(String xmlContent) {// DocumentHelper 解析xml字符串Document document = null;try {document = DocumentHelper.parseText(xmlContent);} catch (DocumentException e1) {e1.printStackTrace();}return document;}

//2.2 转换 将解析出来的数据document 传入转换接口Documentanalysis1 public static List> Documentanalysis1(Document doc) {List> uploadList = new ArrayList>();Element html = doc.getRootElement();// 获取根结点List head = html.elements();Set set = new HashSet<>();head.forEach(a -> {set.add(a.getName());});

set.forEach(a -> {List elements = html.elements(a);// 获取子结点elements.forEach(b -> {Map uploadMap = new HashMap<>();uploadMap.put(b.getName(), b.getText());uploadList.add(uploadMap);});});//返回List>return uploadList;}总结以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:java实现异步线程,回调接口方式Java中Comparable接口和Comparator接口的使用比较Java获取接口的所有实现类方法总结示例java后端如何实现防止接口重复提交解决接口调用报错newSocketStream(..)failed:Too many open files问题

相关文章