加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex与服务器交互之一(URLRequest +URLLoader应用)

发布时间:2020-12-15 04:09:56 所属栏目:百科 来源:网络整理
导读:原文链接 ? ?由于Flex只是一种客户端技术其本身并不能直接同数据库交互,在实际的应用开发过程中Flex提供了如URLRequest、HTTPService、RemoteObject、WebService等类以实现同服务器的通讯和数据交互,下面做一些介绍和实例解析: ?? 1、使用URLRequest向服务

原文链接



? ?由于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包:

[xhtml]? view plain copy
  1. import?mx.rpc.events.FaultEvent;??
  2. import?mx.collections.XMLListCollection;??
  3. import?flash.net.URLLoader;??
  4. import?flash.net.URLRequest;??
  5. import?flash.net.URLVariables;??
  6. import?flash.net.URLRequestMethod;??
  7. import?flash.events.Event;??
  8. import?mx.utils.URLUtil;??
  9. import?mx.collections.ArrayCollection;??
  10. import?mx.rpc.events.ResultEvent;??
  11. import?com.adobe.serialization.json.JSON;??

?? 2、应用示例一:通过URLRequest和URLVariables向服务器发送参数请求并返回服务器文本数据(以下服务器端都采用asp.net编写)
?? 服务器端代码:
[c-sharp]? copy
    protected?void?Page_Load(object?sender,?EventArgs?e)??
  1. {??
  2. ????????//获取URLResuest请求回的参数?返回数据用;分隔以方便Flex对数据进行序列化??
  3. ????????string?rs?=?String.Format("name={0};age={1};address={2}",Request.QueryString["name"],??
  4. ?????????????????????????????????????????????????????????????????Request.QueryString["age"],??
  5. ?????????????????????????????????????????????????????????????????Request.QueryString["address"]);??
  6. ????????Response.ClearContent();??
  7. ????????Response.ContentType?=?"text/plain";??
  8. ????????Response.Write(rs);?//以文本形式返回数据??
  9. ????????Response.End();??
  10. }??

?? FLEX中发送请求:

copy
    private?function?getText():void??
  1. ????var?v:URLVariables?=?new?URLVariables("name=jacky&age=35&address=HongKong?China");??
  2. ????var?r:URLRequest?=?new?URLRequest();??
  3. ????r.url?=?"http://localhost:2222/FlexService/TextFrm.aspx";??
  4. ????r.method?=?URLRequestMethod.GET;??
  5. ????r.data?=?v;??
  6. ?????
  7. ????var?l:URLLoader?=?new?URLLoader();??
  8. ????l.load(r);??
  9. ????l.addEventListener(Event.COMPLETE,txtCompleteHandler);?//注册请求完成后?响应获取数据事件方法??
  10. ????labMsg.alpha?=?1;??
  11. ?? Flex中接收返回数据
    copy
      ???private?function?txtCompleteHandler(e:Event): ????var?l:URLLoader?=?URLLoader(e.target);??
    1. ????var?o:Object?=?URLUtil.stringToObject(String(l.data));?//使用URLUtil对将数据进行反序列化以方便使用??
    2. ????txtName.text?=?o.name;??
    3. ????txtAge.text?=?o.age;??
    4. ????txtAddress.text?=?o.address;??
    5. ????labMsg.alpha?=?0;??
    6. ?? 3、应用示例二:返回JSON格式数据:
      ????
      copy
        public?partial?class?JsonFrm?:?System.Web.UI.Page??
      1. ????{??
      2. ????????Person?person;??
      3. ????????{??
      4. ????????????person?=?new?Person();??
      5. ????????????person.Name?=?"YAO?MING";??
      6. ????????????person.Age?=?29;??
      7. ????????????person.Address?=?"ShangHai?China";??
      8. ????????????HttpContext.Current.Response.ClearContent();??
      9. ????????????HttpContext.Current.Response.ContentType?=?"text/plain";??
      10. ????????????System.Runtime.Serialization.Json.DataContractJsonSerializer?dcjs?=?new?System.Runtime.Serialization.Json.DataContractJsonSerializer(person.GetType());??
      11. ????????????dcjs.WriteObject(HttpContext.Current.Response.OutputStream,person);??
      12. ????????????HttpContext.Current.Response.End();??
      13. ????????}??
      14. ????}??
      15. ????[System.Runtime.Serialization.DataContract]??
      16. ????public?class?Person??
      17. ????{??
      18. ????????[System.Runtime.Serialization.DataMember]??
      19. ????????public?String?Name{get;set;}??
      20. int?Age?{?get;?set;?}??
      21. public?String?Address?{? ????}??
      ???
      copy
        private?function?getJson(): ????????var?v:URLVariables?=?new?URLVariables();??
      1. ????????var?r:URLRequest?=? ????????r.url?=?"http://localhost:2222/FlexService/JsonFrm.aspx";??
      2. ????????r.method?=?URLRequestMethod.GET;??
      3. ????????r.data?=?v;??
      4. ?????????
      5. ????????var?l:URLLoader?=? ????????l.load(r);??
      6. ????????l.addEventListener(Event.COMPLETE,jsonCompleteHandler);??
      7. ????????labMsg.alpha?=?1;??
      8. ????}??
      ??
      copy
        public?function?jsonCompleteHandler(e:Event): ??{??
      1. ??????var?l:URLLoader?=?e.target?as?URLLoader;??
      2. ??????var?o:*?=?JSON.decode(l.data);??
      3. ???????
      4. ??????txtName.text?=?o.Name;??
      5. ??????txtAge.text?=?o.Age;??
      6. ??????txtAddress.text?=?o.Address;??
      7. ??????labMsg.alpha?=?0;??
      8. ??}??
      ? 4、应用示例三:返回XML格式数据:
      ? 服务器端代码:
      copy
        ??????String?s?=?@"<?xml?version=""1.0""?encoding=""utf-8""?>??
      1. ??????????????????????<EV_ChartData_Scatter?xmlns=""http://www.byd.com/ChartData/XML"">??
      2. ????????????????????????<ScatterItem?id=""0"">??
      3. ??????????????????????????<CAN_ITEM_ID>561</CAN_ITEM_ID>??
      4. ??????????????????????????<CAN_ITEM_NAME>主控ECU通讯状况</CAN_ITEM_NAME>??
      5. ??????????????????????????<ITEM_VALUE>0</ITEM_VALUE>??
      6. ??????????????????????????<MATCH_VALUE>正常</MATCH_VALUE>??
      7. ?????????????????????????</ScatterItem>??
      8. ????????????????????????<ScatterItem??id=""1"">??
      9. ??????????????????????????<ITEM_VALUE>1</ITEM_VALUE>??
      10. ??????????????????????????<MATCH_VALUE>丢包</MATCH_VALUE>??
      11. ????????????????????????</ScatterItem>??
      12. ??????????????????????</EV_ChartData_Scatter>";??
      13. ??????Response.ClearContent();??
      14. ??????Response.ContentType?=?"text/xml";??
      15. ??????Response.Write(s);??
      16. ??????Response.End();??
      17. copy
          private?function?getXML(): ????????r.url?=?"http://localhost:2222/FlexService/XMLFrm.aspx";??
        1. copy
            [Bindable]??
          1. private?var?xml:XML;??
          2. private?function?xmlCompleteHandler(e:Event): ????????{??
          3. ????????????var?l:URLLoader?=?e.target? ????????????xml=?new?XML(l.data);??
          4. ????????????for?each(var?v?in?xml.ScatterItem)??
          5. ????????????{??

          (编辑:李大同)

          【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读