本文實例講述了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.jfrwli.cn/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.jfrwli.cn/Add");
//調用該方法
Integer res = (Integer)call.invoke(
new Object[]...{
i, j
}
);
System.out.println( "Result: " + res.toString());
} catch (Exception e) ...{
System.err.println(e);
}
}
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.jfrwli.cn/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.jfrwli.cn/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程序設計有所幫助。