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