本文實例講述了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("//www.VeVB.COm/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("//www.VeVB.COm/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程式設計有幫助。