1 / 18

简单介绍 Flex与Web服务 端 的 交互

简单介绍 Flex与Web服务 端 的 交互. ----by 彭伟林. 简单介绍信 Flex与Web服务的 交互. 主要内容: 1.ActionScript与服务端的基本交互方式(REST). 2.ActionScript特有的数据传输格式(AMF)与服务器交互. 3.Flex与WebService. ActionScript与服务端的基本交互方式. 要实现ActionScript与服务端交互, 首先必须知道以下几个类: URLRequest类 记录请求服务端的URL信息, 给URLLoader进行发送操作. URLVariables类

page
Download Presentation

简单介绍 Flex与Web服务 端 的 交互

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 简单介绍Flex与Web服务端的交互 ----by 彭伟林

  2. 简单介绍信Flex与Web服务的交互 • 主要内容: • 1.ActionScript与服务端的基本交互方式(REST). • 2.ActionScript特有的数据传输格式(AMF)与服务器交互. • 3.Flex与WebService.

  3. ActionScript与服务端的基本交互方式 • 要实现ActionScript与服务端交互, 首先必须知道以下几个类: URLRequest类 记录请求服务端的URL信息, 给URLLoader进行发送操作. URLVariables类 保存向服务端请求所需的参数, 一般用于POST方式传输,GET方式传输不需要使用. URLRequestMethod类 该类只有两个静态常量值, 一个是POST, 一个是GET, 可以把它当作是一个枚举类型, 用于指定URL请求时使用POST方式或是GET方式. URLLoader类 真正与服务端打交道的类, 通过load方法把一个Reuqest发送给服务端, 在load之前必须给该对象添加一个Complete事件的监听器,接收服务器返回的信息. 传输过程中有可能产生IO错误, 有必要时可以为该对象添加IOError监听器.

  4. ActionScript与服务端的基本交互方式 例子: var request: URLRequest = new URLRequest("http://localhost"); //默认为URLRequestMethod.GET, 若使用GET方式可以不指定 request.method = URLRequestMethod.POST; //创建一个参数变量对象,用于保存所有向服务发送的参数数据, GET方式可以不使用 var vars: URLVariables = new URLVariables (); vars["varName"] = varValue;//把参数键,值对放到vars对象中. vars["var2Name"] = var2Value; request.data = vars; //把参数对象放进Request对象中作为参数 // 通过request对象创建一个loader, var loader: URLLoader = new URLLoader(request);

  5. ActionScript与服务端的基本交互方式 loader.addEventListener(//给loader对象添加完成时的监听器. Event.COMPLETE, function(e: Event):void //临时定义一个监听器 { //服务端返回的数据是保存在loader的data里,通过以下语句可以得到 trace(URLLoader(e.target).data );//在监听器里处理服务端返回的数据 //complete // process the data; } ); loader.addEventListener(//IOError监听器 IOErrorEvent.IO_ERROR, //临时监听器,IO错误的处理在该函数中处理 function(e: IOErrorEvent): void {/* process the err;*/} ); loader.load(request);//把request对象发送到服务端.当请求完成,则会自动触发COMPLETE事件

  6. ActionScript与服务端的基本交互方式 REST实例演示

  7. 通过AMF数据格式与服务器交互 • 什么是AMF? AMF是Adobe公司制定一套ActionScript与服务端交互的数据格式, Flex本身已经有相关的类可以直接使用,但服务端则需要一个可以解释该格式的包或框架, 幸运的时现在已经存在大部分服务端语言解释AMF格式包与类库, 所以不用担心处理该格式的问题. • AMF有什么优势?为什么要使用AMF? 原因:AMF数据格式采用了二进制数据压缩, 然后再把数据放送到服务端进行处理, 虽然采用了压缩技术,但并不影响效率, 而传统的XML-RPC传输大量的XML标签, 大大的影响了传输的速度, 且增大传输的流量.AMF则不存在这样的问题.所以AMF主要的优势是采用二进制传输,传输的数据量较少, 大大提升交互的速度.

  8. 通过AMF数据格式与服务器交互 • 怎样在Flex中使用AMF数据格式进行传输? 在Flex中使用AMF需要通过以下几个类进行: RemoteObject类, 可以作为Flex中的一个控件使用. AsyncToken类, 保存一个请求的状态, 给请求添加监听器. AsyncResponder类, 用于设置请求的两种返回结果, 返回成功或失败 ResultEvent类, 返回成功的事件对象, 返回的数据保存在该对象的result属性里. FaultEvent类, 返回失败的事件对象, 该对象保存请求失败的所有信息.

  9. 通过AMF数据格式与服务器交互 • 例子(后台采用django + pyAMF): 在Application的MXML文件里创建一个RemoteObject对象 <mx:RemoteObjectid="djangoService"destination="myservice" showBusyCursor="true"/> Flex编写以下代码: //djangoService是面上创建一个RemoteObject对象, echo是服务端提供的一个无参数方法, 用于显示信息. var token:AsyncToken=djangoService.echo(); //调用服务端的方法后返回一个Token对象 // 为Token添加成功与失败的监听器. token.addResponder(new AsyncResponder(AfterEchoRet,falutHandler));

  10. 通过AMF数据格式与服务器交互 //字义成功与失败的监听器. private function AfterEchoRet(result:ResultEvent):void{ trace(result.result); //在这里处理返回的数据.数据保存于result属性中. } private function falutHandler(error:FaultEvent):void{ trace(error.message); } 程序代码就只有这么多, 不过还需要一个Services-config.xml配置文件. 还需要在工程属性里添加一个参数, 设置如下:

  11. 通过AMF数据格式与服务器交互 services-config.xml内容如下: <?xml version="1.0" encoding="UTF-8"?> <services-config> <services> <service id="ananasService" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage"> <destination id="myservice">//需要对应pyAMF服务器上设定的destination 值,必须一致 <channels><channel ref="ananasChannel"/></channels> //对应下面定义的Channel <properties><source>*</source></properties> </destination> </service> </services> <channels> <channel-definition id="ananasChannel" class="mx.messaging.channels.AMFChannel"> <endpoint uri="http://192.168.28.97/pysite/hello/gateway/" class="flex.messaging.endpoints.AMFEndpoint"/> //设置服务器终端URL地址 </channel-definition> </channels> </services-config>

  12. 通过AMF数据格式与服务器交互 AMF实例演示

  13. Flex与WebService • WSDL(Web Service Description Language)Web服务器描述语言是用XML文档来描述Web服务的标准,是Web服务的接口定义语言,通过WSDL,可描述Web服务的三个基本属性: ·服务做些什么——服务所提供的操作(方法) ·如何访问服务——和服务交互的数据格式以及必要协议 ·服务位于何处——协议相关的地址,如URL • Flex本身就拥有支持WebService的类, 常用的是: WebService类,该类已经可以实现对服务端提供的WebService的WSDL数据进行相应的操作.

  14. Flex与WebService Flex framework 包含mx.rpc.soap.WebService,该类可调用web services方法,首先创建WebService 对象,如下: var webService:WebService = new WebService( ); 每个web service都有一个Web服务描述语言(WSDL),通过WebService对象的wsdl属性进行定位: webService.wsdl = "http://www.rightactionscript.com/webservices/FlashSurvey.php?wsdl"; 在调用方法之前,必须先用loadWSDL( ) 方法读取wsdl数据: webService.loadWSDL( ); loadWSDL( ) 方法是异步调用的,因此需要监听是否WSDL数据已经读取完毕,当数据读取完成 时WebService 对象会发出mx.rpc.soap.LoadEvent 事件,如: webService.addEventListener(LoadEvent.LOAD, onWSDL); 当WSDL数据读取后,就可以调用WebService对象方法了,WSDL URL 指向真实的web service ,其有个方法叫getAverages( ) : webService.getAverages( );

  15. Flex与WebService 处理返回结果 Web services 方法的类型实际上是mx.rpc.soap.Operation,当web services 方法返回值时,方法对象会发出mx.rpc.events.ResultEvent 事件,要想处理这个事件可注册监听器,例如,webService有个方法叫getAverages( ),可这样注册监听器: webService.getAverages.addEventListener(ResultEvent.RESULT, onGetAverages); 调用写法和其他方法一样: webService.getAverages( ); 当进入处理函数时,会传进来一个ResultEvent 参数,ResultEvent 类定义了一个叫result 的属性,它包含返回值,因为getAverages( ) 方法返回一个关联数组,包含两个属性:flash 和actionscript: private function onGetAverages(event:ResultEvent):void { textArea.text = "The averages for Flash and ActionScript are " + event.result.flash + " and " + event.result.actionscript; }

  16. Flex与WebService 处理异常 当web services 引发异常时,方法会发出fault事件,类型为mx.rpc.events.FaultEvent,下面的代码注册了fault 事件处理函数: webService.addEventListener(FaultEvent.FAULT, onWebServiceFault); FaultEvent 类定义了一个fault属性,类型为mx.rpc.Fault. Fault 对象返回有关异常的信息,包含如faultCode, faultDetail, faultString, 和rootCause 属性,下面的例子用Alert 显示异常信息: private onWebServiceFault(event:FaultEvent):void { var fault:Fault = FaultEvent.fault; var message:String = "An error occurred. The details are as follows\ncode: " + fault.faultCode; message += "\ndetail: " + faul.faultDetail; Alert.show("Web Service Error", message); }

  17. Flex与WebService WebService实例演示

  18. 谢谢大家!

More Related