先看一段代码:
var ws:WebService ;
//调用WebService服务端发布的login(username,password)方法
private function doLogin():void{
ws = new WebService();
ws.wsdl = "http://localhost:9090/HelloWorld/LoginService?wsdl";
ws.loadWSDL();
//调用login方法,方法参数为username,password
ws.login.addEventListener(ResultEvent.RESULT,loginSuccessHandler);
ws.login.addEventListener(FaultEvent.FAULT,loginFailHandler);
ws.login("meteor","123");
}
private function loginSuccessHandler(event:ResultEvent):void{
Alert.show("Login Successful!");
}
private function loginFailHandler(event:FaultEvent):void{
Alert.shor("Login fail!");
}
//调用WebService服务端发布的getInfo()方法
private function doLogin():void{
ws = new WebService();
ws.wsdl = "http://localhost:9090/HelloWorld/getInfoWervice?wsdl";
ws.loadWSDL();
//调用getInfo方法
ws.getInfo.addEventListener(ResultEvent.RESULT,getInfoHandler);
ws.getInfo.loginaddEventListener(FaultEvent.FAULT,faultHandler);
ws.getInfo();
}
private function getInfoHandler(event:ResultEvent):void{
Alert.show("Get info Successful!");
}
private function faultHandler(event:FaultEvent):void{
Alert.shor(event.fault.message);
}
以上代码中存在很多重复片段:每次调用WebService,都要写重复的代码。下面来创建一个动态的WebService服务类
在mxml中调用
var ws:BaseWebService;
private function login():void{
ws = new BaseWebService();
ws.initWsdl("http://localhost:9090/HelloWorld/getInfoWervice?wsdl");
ws.sendOperation("login");
ws.callBackHandler = loginSuccessHandler;
ws.faultHandler = loginFailHandler;
var arr:Array = new Array();
arr.push("meteor");
arr.push("123");
ws.sendOperation("login",arr);
}