由于Flex只是一种客户端技术其本身并不能直接同数据库交互,在实际的应用开发过程中Flex提供了如URLRequest、HTTPService、RemoteObject、WebService等类以实现同服务器的通讯和数据交互,下面做一些介绍和实例解析:
1、使用URLRequest向服务器发送请求,使用URLLoader接收服务器返回的数据:
URLRequest类可捕获单个 HTTP 请求中的所有信息,可以将 URLRequest 对象传递给 Loader、URLStream 和 URLLoader 类以及其他加载操作的 load() 方法以启动 URL 下载;默认情况下,传递给 url 参数的 URL 必须与执行调用的 SWF 文件在完全相同的域,包括子域。如果要从其它域中加载数据,请在承载数据的服务器上放置一个跨域策略文件。有关URLRequest详细可参看http://help.adobe.com/zh_CN/AIR/1.1/jslr/flash/net/URLRequest.html
URLVariables类主要用来在应用程序和服务器之间传输参数变量;详细查看:http://livedocs.adobe.com/flex/3_cn/langref/flash/net/URLVariables.html
URLLoader 类以文本、XML、二进制数据或 URL 编码变量的形式从 URL 返回请求的数据详细请参看:http://livedocs.adobe.com/flex/3_cn/langref/flash/net/URLLoader.html
完成以下示例要引用到的Flex包:
- import mx.rpc.events.FaultEvent;
- import mx.collections.XMLListCollection;
- import flash.net.URLLoader;
- import flash.net.URLRequest;
- import flash.net.URLVariables;
- import flash.net.URLRequestMethod;
- import flash.events.Event;
- import mx.utils.URLUtil;
- import mx.collections.ArrayCollection;
- import mx.rpc.events.ResultEvent;
- import com.adobe.serialization.json.JSON;
2、应用示例一:通过URLRequest和URLVariables向服务器发送参数请求并返回服务器文本数据(以下服务器端都采用asp.net编写)
服务器端代码:
- protected void Page_Load(object sender, EventArgs e)
- {
-
- string rs = String.Format("name={0};age={1};address={2}",Request.QueryString["name"],
- Request.QueryString["age"],
- Request.QueryString["address"]);
- Response.ClearContent();
- Response.ContentType = "text/plain";
- Response.Write(rs);
- Response.End();
- }
FLEX中发送请求:
- private function getText():void
- {
- var v:URLVariables = new URLVariables("name=jacky&age=35&address=HongKong China");
- var r:URLRequest = new URLRequest();
- r.url = "http://localhost:2222/FlexService/TextFrm.aspx";
- r.method = URLRequestMethod.GET;
- r.data = v;
-
- var l:URLLoader = new URLLoader();
- l.load(r);
- l.addEventListener(Event.COMPLETE,txtCompleteHandler);
- labMsg.alpha = 1;
- }
Flex中接收返回数据
- private function txtCompleteHandler(e:Event):void
- {
- var l:URLLoader = URLLoader(e.target);
- var o:Object = URLUtil.stringToObject(String(l.data));
- txtName.text = o.name;
- txtAge.text = o.age;
- txtAddress.text = o.address;
- labMsg.alpha = 0;
- }
3、应用示例二:返回JSON格式数据:
服务器端代码:
- public partial class JsonFrm : System.Web.UI.Page
- {
- Person person;
- protected void Page_Load(object sender, EventArgs e)
- {
- person = new Person();
- person.Name = "YAO MING";
- person.Age = 29;
- person.Address = "ShangHai China";
- HttpContext.Current.Response.ClearContent();
- HttpContext.Current.Response.ContentType = "text/plain";
- System.Runtime.Serialization.Json.DataContractJsonSerializer dcjs = new System.Runtime.Serialization.Json.DataContractJsonSerializer(person.GetType());
- dcjs.WriteObject(HttpContext.Current.Response.OutputStream,person);
- HttpContext.Current.Response.End();
- }
- }
- [System.Runtime.Serialization.DataContract]
- public class Person
- {
- [System.Runtime.Serialization.DataMember]
- public String Name{get;set;}
- [System.Runtime.Serialization.DataMember]
- public int Age { get; set; }
- [System.Runtime.Serialization.DataMember]
- public String Address { get; set; }
- }
FLEX中发送请求:
- private function getJson():void
- {
- var v:URLVariables = new URLVariables();
- var r:URLRequest = new URLRequest();
- r.url = "http://localhost:2222/FlexService/JsonFrm.aspx";
- r.method = URLRequestMethod.GET;
- r.data = v;
-
- var l:URLLoader = new URLLoader();
- l.load(r);
- l.addEventListener(Event.COMPLETE,jsonCompleteHandler);
- labMsg.alpha = 1;
- }
Flex中接收返回数据
- public function jsonCompleteHandler(e:Event):void
- {
- var l:URLLoader = e.target as URLLoader;
- var o:* = JSON.decode(l.data);
-
- txtName.text = o.Name;
- txtAge.text = o.Age;
- txtAddress.text = o.Address;
- labMsg.alpha = 0;
- }
4、应用示例三:返回XML格式数据:
服务器端代码:
- protected void Page_Load(object sender, EventArgs e)
- {
- String s = @"<?xml version=""1.0"" encoding=""utf-8""?>
- <EV_ChartData_Scatter xmlns=""http:
- <ScatterItem id=""0"">
- <CAN_ITEM_ID>561</CAN_ITEM_ID>
- <CAN_ITEM_NAME>主控ECU通讯状况</CAN_ITEM_NAME>
- <ITEM_VALUE>0</ITEM_VALUE>
- <MATCH_VALUE>正常</MATCH_VALUE>
- </ScatterItem>
- <ScatterItem id=""1"">
- <CAN_ITEM_ID>561</CAN_ITEM_ID>
- <CAN_ITEM_NAME>主控ECU通讯状况</CAN_ITEM_NAME>
- <ITEM_VALUE>1</ITEM_VALUE>
- <MATCH_VALUE>丢包</MATCH_VALUE>
- </ScatterItem>
- </EV_ChartData_Scatter>";
- Response.ClearContent();
- Response.ContentType = "text/xml";
- Response.Write(s);
- Response.End();
- }
FLEX中发送请求:
- private function getXML():void
- {
- var v:URLVariables = new URLVariables();
- var r:URLRequest = new URLRequest();
- r.url = "http://localhost:2222/FlexService/XMLFrm.aspx";
- r.method = URLRequestMethod.GET;
- r.data = v;
-
- var l:URLLoader = new URLLoader();
- l.load(r);
- l.addEventListener(Event.COMPLETE,xmlCompleteHandler);
- }
Flex中接收返回数据
- [Bindable]
- private var xml:XML;
- private function xmlCompleteHandler(e:Event):void
- {
- var l:URLLoader = e.target as URLLoader;
- xml= new XML(l.data);
- for each(var v in xml.ScatterItem)
- {
-
- }
- labMsg.alpha = 0;
- }