1.
其中sendSyncMsg1接口是方法名,Vector實現了List接口,xml是sendSyncMsg1的方法形參
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL( "http://127.0.0.1:8080/services/fangkunTest"));
call.setOperationName(new QName("http://127.0.0.1:8080/", "sendSyncMsg1"));
call.addParameter(new QName("http://127.0.0.1:8080/", "xml"), XMLType.XSD_STRING, ParameterMode.IN); //必須這么用,不知道為啥,此處與返回基本類型寫法有差別
call.setReturnType(XMLType.SOAP_VECTOR); //返回時數組類型,暫時知道用XMLType.SOAP_VECTOR
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://127.0.0.1:8080/sendSyncMsg1");
Vector result = (Vector) call.invoke(new Object[] { "昆明" });
// Vector實現了List接口
System.out.println((String) result.get(1));
if (result != null && result.size() != 0) {
if (result.get(0).equals("1")) {
String etoken = (String) result.get(1);
} else {
System.out.println(result.get(2));
return;
}
}
2.(轉來自google)
准備工作:
主要依賴的包:
1.axis.jar
官網:http://axis.apache.org/axis/
2.jaxrpc.jar
下載地址:http://www.java2s.com/Code/Jar/j/Downloadjaxrpcjar.htm
說明:
在拿到wsdl地址后先分析清楚xml文件的一些參數,然后再動手寫代碼,例如service名稱,targetNamespace,請求operation的名稱,要傳入的參數類型,返回結果類型等等。具體的代碼參考如下:
1 package com.joysee.account.service.impl; 2 3 4 import java.rmi.RemoteException; 5 6 import javax.xml.namespace.QName; 7 import javax.xml.rpc.ParameterMode; 8 import javax.xml.rpc.ServiceException; 9 10 import org.apache.axis.client.Call; 11 import org.apache.axis.client.Service; 12 13 14 public class Test { 15 16 /** 17 * @param args 18 * @throws ServiceException 19 * @throws MalformedURLException 20 * @throws RemoteException 21 */ 22 public static void main(String[] args) throws ServiceException, RemoteException { 23 24
常見異常:
1.org.xml.sax.SAXException: No deserializer for {http://www.w3.org/2001/XMLSchema}anyType
這個異常需要檢查call.setReturnClass(); 中的類型,或者是setReturnType()
2. Unexpected wrapper element order found.
可以檢查setOperationName中的QName參數,其中的NameSpace地址是不是有問題,或者operation名是否有誤