欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

axis调用webservice相关工具类

程序员文章站 2022-06-07 11:26:08
...
1、axis调用webservice的方法
public static String getUnionCicWebserviceResult(String URL,String nameSpaceURL, String webMethod,Object[] params) {
                        org.apache.axis.client.Service s = new org.apache.axis.client.Service();
                        Call call;
                        String val = "";
                        try {
                                    call = (Call) s.createCall();
                                    call.setTargetEndpointAddress(new java.net.URL(URL));
                                    call.setOperationName(new QName(nameSpaceURL, webMethod));
                                    for (int i = 0; i < params.length; i++) {
                                                call.addParameter("arg" + i,org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);// 此处参数名字必须为arg0,arg1形式
                                    }
                                    call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
                                    val = (String) call.invoke(params);
                        } catch (javax.xml.rpc.ServiceException e) {
                                    e.printStackTrace();
                                    return val;
                        } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                    return val;
                        } catch (RemoteException e) {
                                    e.printStackTrace();
                                    return e.getMessage();
                        }
                        return val;
            }
2、解析webservice返回的xml格式的方法
/**
             * 解析华西联合门诊返回的xml字符串
             * @param xmlStr
             * @return Map<String,Object>
             */
            @SuppressWarnings("unchecked")
            public static Map<String,Object> getUnionCicResponseMap(String xmlStr) {
                        Document doc = null;
                        Map<String,Object> map = new HashMap<String, Object>();
            try {
                                                doc = DocumentHelper.parseText(xmlStr);
            // 将字符串转为XML
            Element rootElt = doc.getRootElement();
            List<Element> childElements = rootElt.elements();
                        for (Element ele:childElements) {
            //                     if("acknowledgement".equals(ele.getName())){
                                                //解析xml格式标签中的属性
                                                List attrList = ele.attributes();
                                                for (int i = 0; i < attrList.size(); i++) {
                                                    Attribute item = (Attribute)attrList.get(i);
                                                    map.put(item.getName(), item.getValue());
                                                }                       
            //                     }
                                                            map.put(ele.getName(), ele.getStringValue());
                                                }
            } catch (DocumentException e) {
                        map.put("typeCode", "AE");
                        map.put("acknowledgement", e.getMessage());
                        e.printStackTrace();
                        return map;
            }
        return map;
    }
3、将数据库查询结果封装为json格式
StringBuilder sb = new StringBuilder();
                                                // 拼装病人信息
                                                for (int i = 0; i < inofList.size(); i++) {
                                                            sb.append("{");
                                                            final Set<String> keys = inofList.get(i).keySet();
                                                            for (final String key : keys) {
                                                                        final Object value = inofList.get(i).get(key);
                                                                        sb.append("\"");
                                                                        sb.append(key);
                                                                        sb.append("\"");
                                                                        if (value != null || !"null".equals(value)) {
                                                                                    sb.append(":\"");
                                                                                    sb.append(value);
                                                                                    sb.append("\",");
                                                                        } else {
                                                                                    sb.append(":\"\",");
                                                                        }
                                                            }
                                                            sb.setCharAt(sb.length() - 1, '}');
                                                            sb.append(",");
                                                }
                                                sb.setCharAt(sb.length() - 1, ' ');
                                                json = "{\"status\":1,\"info\":[" + sb + "]}";