本文執行個體講述了Java調用WebService介面的方法。分享給大家供大家參考。具體如下:
這裡講述有參方法Add,代碼如下:
複製代碼 代碼如下:
public static void addTest() {
try ...{
Integer i = 1;
Integer j = 2;
//WebService URL
String service_url = "http://localhost:4079/ws/Service.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(service_url));
//設定要調用的方法
call.setOperationName(new QName("http://www.jb51.net/T", "Add"));
//該方法需要的參數
call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT,
javax.xml.rpc.ParameterMode.IN);
//方法的傳回值類型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://www.jb51.net/Add");
//調用該方法
Integer res = (Integer)call.invoke(
new Object[]...{
i, j
}
);
System.out.println( "Result: " + res.toString());
} catch (Exception e) ...{
System.err.println(e);
}
}
運行,結果返回:Result:3
希望本文所述對大家的Java程式設計有所協助。