Flash与后台数据交互---WebService篇
Flash框架没有提供WebServices功能,因此要想用Flash来直接调用WebServices方法将会有一定的难度。
不过,幸运的是同样是Adobe公司提供的Flex框架包含了一种解决方案,通过使用Flex框架可以很方便的调用WebServices方法。
通过添加类库的方法可以使Flash能够使用Flex框架,使得Flash访问WebServices变得非常简单。
Flash添加类库的方法详见http://www.cnblogs.com/botsing/archive/2009/02/20/1394891.html。
本处采用在首选参数中添加类路径的方法。
通过使用mx.rpc.soap.WebService对象,然后从WebService对象来调用方法。
package { import flash.display.MovieClip; import mx.rpc.soap.LoadEvent; import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; import flash.events.MouseEvent; import mx.rpc.soap.WebService; public class WebServicesSample extends MovieClip { private var service:WebService; public function WebServicesSample() { LoginPanel.btn_login.addEventListener(MouseEvent.CLICK,login); //初始化WebService变量,并给wsdl属性赋值。wsdl属性将告诉WebService对象到哪里去找WSDL。 service=new WebService(); service.wsdl="http://localhost:6558/LoginSrv.asmx?WSDL"; } private function login(evt:MouseEvent):void { //在调用WebService方法前必须先调用loadWSDL方法,从指定的URL加载WSDL数据。 service.loadWSDL(); //给WebService对象添加侦听器,监听WSDL加载完成。 service.addEventListener(LoadEvent.LOAD,onWSDL); //给WebService对象添加侦听器,监听出错的情况。 service.addEventListener(FaultEvent.FAULT,onFault); } private function onWSDL(evt:LoadEvent):void { //为WebService方法添加侦听器,监听调用结果返回的情况。 service.Login.addEventListener(ResultEvent.RESULT,onLogin); //为WebService方法添加侦听器,监听出错的情况。 service.Login.addEventListener(FaultEvent.FAULT,onFault); //调用WebService方法。 service.Login(LoginPanel.tb_username.text,LoginPanel.tb_psw.text); } private function onLogin(evt:ResultEvent):void { if(evt.result) { LoginPanel.gotoAndStop(2); LoginPanel.btn_login.removeEventListener(MouseEvent.CLICK,login); LoginPanel.btn_login.addEventListener(MouseEvent.CLICK,logout); LoginPanel.btn_login.label_btn.text="退出"; } else { LoginPanel.gotoAndStop(3); LoginPanel.btn_login.removeEventListener(MouseEvent.CLICK,login); LoginPanel.btn_login.addEventListener(MouseEvent.CLICK,logout); LoginPanel.btn_login.label_btn.text="返回"; } } private function logout(evt:MouseEvent):void { LoginPanel.gotoAndStop(1); LoginPanel.btn_login.removeEventListener(MouseEvent.CLICK,logout); LoginPanel.btn_login.addEventListener(MouseEvent.CLICK,login); LoginPanel.btn_login.label_btn.text="登录"; } private function onFault(evt:FaultEvent):void { //出错处理语句 trace("出错了!"); } } }
具体可以查看例子源码。
本文中使用的部分资源下载:
Flex 3.2.0 frameworks 类库源文件:
http://cid-f181d7835b5948b9.skydrive.live.com/self.aspx/Flex%7C_Source/FlexFramework%7C_3%7C_2%7C_0%7C_Source.rar
Flex 3.2.0 rpc 类库源文件:
http://cid-f181d7835b5948b9.skydrive.live.com/self.aspx/Flex%7C_Source/FlexRPC%7C_3%7C_2%7C_0%7C_Source.rar
WebServiceSample.rar:
http://cid-f181d7835b5948b9.skydrive.live.com/self.aspx/Flex%7C_Source/WebServicesSample.rar

浙公网安备 33010602011771号