Flex与服务器交互之一(URLRequest +URLLoader应用)
由于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)
{
//获取URLResuest请求回的参数 返回数据用;分隔以方便Flex对数据进行序列化
string rs = String.Format("name={0};age={1};address={2}",Request.QueryString["name"],
相关文档:
<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate the DateTimeAxis class. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
import mx.collections.ArrayCollection;
......
相关下载
Flex正式版EXE下载地址:
http://trials.adobe.com/Applications/Flex/FlexBuilder/3/FB3_WWEJ.exe
Flex正式版插件下载地址:
http://trials.adobe.com/Applications/Flex/FlexBuilder/3/FB3_WWEJ_Plugin.exe
LCDS
官方下载(需要先注册)
https://www.adobe.com/cfusion/tdrc/index.cfm?p ......
常用的Flex代码总结
1.刷新浏览器
navigateToURL(new URLRequest("javascript:location.reload();"),"_self")
2.关闭浏览器
navigateToURL(new URLRequest("javascript:window.close()"),"_self");
3.打开一个新浏览器窗口
navigateToURL(new URLRequest('ht ......
页面代码:
<?xml version="1.0"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:marsImage="mars.display.*"
backgroundColor="#000000"
layout="absolute">
<marsImage:BorderImage
borderAlpha="{aBar.value ......