Flex---->连接WebService
Flex连接后台数据有四种:WebService、HTTPService、RemoteObject、Socket,其中Webservice和HTTPService数据传输格式都是XML,数据传输量较大,传输效率不高,但是也不失为一种可行的解决方案,后两种RemoteObject和Socket都传输的是二进制数据传输效率比较高,RemoteObject在企业级RIA应用中较为常用,而Socket可能更多的是用于游戏中。
1、Flex连接WebService的例子
<?xml version="1.0" encoding="utf-8"?> <!-- (C) aisajiajiao 2011 Flex调用WebService --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; import mx.rpc.soap.LoadEvent; import mx.rpc.soap.WebService; import mx.controls.Alert; private var str:String = "http://webservice.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl"; private var webService:WebService; private function onClick():void { //创建并实例化WebService示例,不过之前要先导入WebService类 webService = new WebService(); webService.wsdl = str; webService.loadWSDL(str); //事件侦听我就不说了,文档上都有 webService.addEventListener(LoadEvent.LOAD, onLoad); webService.addEventListener(ResultEvent.RESULT, onResult); webService.addEventListener(FaultEvent.FAULT, onFault); } private function onLoad(e:LoadEvent):void { //进行相关的操作 webService.toTraditionalChinese(txt.text.toString()); } private function onResult(e:ResultEvent):void { txt.text = e.result.toString(); } private function onFault(e:FaultEvent):void { trace(e.message); //给用警告,偷懒不愿debug的时候这么来 Alert.show("未能连接到WebService,请检查网络!","错误"); } ]]> </mx:Script> <mx:Button label="点击开始转换" click="onClick()" fontSize="48" /> <mx:Label id="txt" text="点击将简体字变成台湾正体字" fontSize="48" /> </mx:Application>