Java使用Axis2调用Web services接口(二)

package com.ibm.hlx;
/**
 * 用RPCServiceClient方式调用
 * @author John
 *
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class TestClient02_test {

    public static void main(String[] args) throws AxisFault {
        String url = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?WSDL";
        String action = "http://WebXml.com.cn/TranslatorString";
        String methodStr = "TranslatorString";
        String namespace = "http://WebXml.com.cn/";
        String tns = "";
        String[] pars = {"wordKey"};
        String[] vals = {"何"};
        OMElement result = null;
        result = translatorString(url,action, methodStr, namespace, tns, pars, vals);
        System.out.println(getResults(result));
        //System.out.println(result);
    }

    public static OMElement translatorString(String url,String action,String methodStr,String namespace,String tns,String [] pars,String [] vals){
        OMElement result = null;
        try {
            RPCServiceClient serviceClient = new RPCServiceClient();
            serviceClient.setOptions(getClientOptions(url,action));
            result = serviceClient.sendReceive(getOMMethod(methodStr,namespace,tns,pars,vals));
        } catch (AxisFault e) {
            e.printStackTrace();
        }
        return result;
    }

    public static Options getClientOptions(String url,String action){

        EndpointReference targetEpr = new EndpointReference(url);
        //创建request soap包 请求选项
        Options options = new Options();
        //设置options的soapAction
        options.setAction(action);
        //设置request soap包的端点引用(接口地址)
        options.setTo(targetEpr);
        //设置超时时间
        options.setTimeOutInMilliSeconds(6000000000L);
        //传输协议
        options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
        return options;
    }
    
    public static OMElement getOMMethod(String methodStr,String namespace,String tns,String [] pars,String [] vals){
        //由抽象OM工厂获取OM工厂,创建request SOAP包
        OMFactory fac = OMAbstractFactory.getOMFactory();
        //创建命名空间
        OMNamespace nms = fac.createOMNamespace(namespace, tns);
        //创建OMElement方法 元素,并指定其在nms指代的名称空间中
        OMElement method = fac.createOMElement(methodStr,nms);
        //添加方法参数名和参数值
        for(int i=0;i<pars.length;i++){
            //创建方法参数OMElement元素
            OMElement param = fac.createOMElement(pars[i],nms);
            //设置键值对 参数值
            param.setText(vals[i]);
            //将方法元素 添加到method方法元素中
            method.addChild(param);
            method.build();
        }
        return method;
    }

    //解析XML,将获取到的数据封装到list中
    public static List<String> getResults(OMElement element){
        if(element == null){
            return null;
        }
        Iterator iterator = element.getChildElements();//获取element子元素
        Iterator innerIter;
        List<String> list = new ArrayList<String>();
        OMElement result = null;
        while(iterator.hasNext()){//判断iterator当前元素是否存在,并指向下一个元素
            result = (OMElement) iterator.next();//返回iterator当前元素,并指向下一个元素
            innerIter = result.getChildElements();//获取result子元素
            while(innerIter.hasNext()){
                OMElement result2 = (OMElement) innerIter.next();
                if(result2 != null){
                    String text = result2.getText();//获取result2的文本内容
                    if(text != null && !("").equals(text)){
                        list.add(text);
                    }
                }
            }
        }
        return list;
    }
}

猜你喜欢

转载自blog.csdn.net/helixue2012/article/details/50589050
今日推荐