产品手册 问题解答

Java调用WebService示例

分类:二次开发 产品中心 2322

package com.hj.services.webservices;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class WebServicesClient {
public static void main(String[] args) throws Exception {
Service sv = new Service(); //new 一个服务
Call call = (Call) sv.createCall(); //创建一个call对象
call.setTargetEndpointAddress(new URL(“http://192.168.200.39:8080/creazy”)); //设置要调用的接口地址以上一篇的为例子
call.setOperationName(new QName(“getUsers”)); //设置要调用的接口方法
call.addParameter(“id”, org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);//设置参数名 id 第二个参数表示String类型,第三个参数表示入参
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); //返回参数类型
// 开始调用方法,假设我传入的参数id的内容是1001 调用之后会根据id返回users信息,以xml格式的字符串返回,也可以json格式主要看对方用什么方式返回
String result = (String) call.invoke(new Object[]{“1001”});
System.out.println(result);//打印字符串
Document doc = DocumentHelper.parseText(result); //转成Document对象
Element root = doc.getRootElement(); //用dom4j方式拿到xml的根节点然后打印结果信息
System.out.println(“id=”+root.element(“UsersID”).getText()+” name=”+root.element(“UsersName”).getText()+” sex=”+root.element(“UsersSex”).getText());

}
}

上一篇: 下一篇:
展开更多
软件测试

loading...