Flex3加载外部数据1
1.加载文本文件中的纯文本:
LoadData1.mxml
<?xml version="1.0" encoding="utf-8"?>
<!--通过Bind绑定,使用了MXML的httpservice标签,结果格式为text文本格式-->
<mx:Application xmlns:mx="Http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="txtLoader.send();">
<mx:Style>
Application{font-size:12px;}
</mx:Style>
<mx:HTTPService id="txtLoader" url="test1.txt" resultFORMat="text"/>
<mx:VBox>
<mx:Text text="{txtLoader.lastResult}" />
</mx:VBox>
</mx:Application>
<!--通过Bind绑定,使用了MXML的httpservice标签,结果格式为text文本格式-->
<mx:Application xmlns:mx="Http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="txtLoader.send();">
<mx:Style>
Application{font-size:12px;}
</mx:Style>
<mx:HTTPService id="txtLoader" url="test1.txt" resultFORMat="text"/>
<mx:VBox>
<mx:Text text="{txtLoader.lastResult}" />
</mx:VBox>
</mx:Application>
文本文件test1.txt:
这是用于Flex的一段测试文本
运行结果:
2.这里不是用MXML标签,而采用类
<?xml version="1.0" encoding="utf-8"?>
<!--使用HTTPService类得到同样的结果-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="loadTxt(event);">
<mx:Style>
Application{fontSize:14px;}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.events.FlexEvent;
import mx.rpc.http.HTTPService;
private var txtLoader:HTTPService=new HTTPService();
//加载文本代码如下
private function loadTxt(evt:FlexEvent):void
{
txtLoader.url="test1.txt";//文件路径
txtLoader.resultFormat="text";//结果的格式是纯文本
txtLoader.addEventListener(ResultEvent.RESULT,resultHandler);
txtLoader.send();
}
private function resultHandler(evt:ResultEvent):void
{
myTxt.text=evt.target.lastResult;//这里为文本控件赋值
}
]]>
</mx:Script>
<mx:VBox>
<mx:Text id="myTxt"/>
</mx:VBox>
</mx:Application>
<!--使用HTTPService类得到同样的结果-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="loadTxt(event);">
<mx:Style>
Application{fontSize:14px;}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.events.FlexEvent;
import mx.rpc.http.HTTPService;
private var txtLoader:HTTPService=new HTTPService();
//加载文本代码如下
private function loadTxt(evt:FlexEvent):void
{
txtLoader.url="test1.txt";//文件路径
txtLoader.resultFormat="text";//结果的格式是纯文本
txtLoader.addEventListener(ResultEvent.RESULT,resultHandler);
txtLoader.send();
}
private function resultHandler(evt:ResultEvent):void
{
myTxt.text=evt.target.lastResult;//这里为文本控件赋值
}
]]>
</mx:Script>
<mx:VBox>
<mx:Text id="myTxt"/>
</mx:VBox>
</mx:Application>
结果同上。
3.使用URLLoader类加载数据
<?xml version="1.0" encoding="utf-8"?>
<!--使用URLLoader类从文本文件加载文本数据到Flex中,无法通过MXML标签使用该类-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="loadTxt(event)">
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
private var loader:URLLoader=new URLLoader();
private function loadTxt(evt:FlexEvent):void
{
loader.addEventListener(Event.COMPLETE,handleData);
loader.load(new URLRequest("test1.txt"));
}
//数据加载完毕,为文本赋值
private function handleData(evt:Event):void
{
myTxt.text=evt.target.data;//URLLoader对象的data域
}
]]>
</mx:Script>
<mx:VBox>
<mx:Text id="myTxt" fontSize="18"/>
</mx:VBox>
</mx:Application>
<!--使用URLLoader类从文本文件加载文本数据到Flex中,无法通过MXML标签使用该类-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="loadTxt(event)">
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
private var loader:URLLoader=new URLLoader();
private function loadTxt(evt:FlexEvent):void
{
loader.addEventListener(Event.COMPLETE,handleData);
loader.load(new URLRequest("test1.txt"));
}
//数据加载完毕,为文本赋值
private function handleData(evt:Event):void
{
myTxt.text=evt.target.data;//URLLoader对象的data域
}
]]>
</mx:Script>
<mx:VBox>
<mx:Text id="myTxt" fontSize="18"/>
</mx:VBox>
</mx:Application>
结果同上。
4.加载url编码值对数据:
<?xml version="1.0" encoding="utf-8"?>
<!--使用HTTPService标签,加载值对(url编码的)到Flex的标签和组合框控件-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="svc.send();">
<mx:Style>
Application{fontSize:14px;}
</mx:Style>
<mx:HTTPService id="svc" url="test2.txt" resultFormat="flashvars"/>
<mx:HBox>
<mx:Label text="{svc.lastResult.lbl}"/>
<mx:ComboBox dataProvider="{svc.lastResult.jobs.split(',')}" />
</mx:HBox>
</mx:Application>
<!--使用HTTPService标签,加载值对(url编码的)到Flex的标签和组合框控件-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="svc.send();">
<mx:Style>
Application{fontSize:14px;}
</mx:Style>
<mx:HTTPService id="svc" url="test2.txt" resultFormat="flashvars"/>
<mx:HBox>
<mx:Label text="{svc.lastResult.lbl}"/>
<mx:ComboBox dataProvider="{svc.lastResult.jobs.split(',')}" />
</mx:HBox>
</mx:Application>
test2.txt:
lbl=职位&jobs=网页设计师,软件工程师,项目经理
结果:
5.使用HTTPService类加载值对:
<?xml version="1.0" encoding="utf-8"?>
<!--使用HTTPService类,加载URL编码数据-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="loadVal(event);">
<mx:Style>
Application{fontSize:12px;color:#ff0000;}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.events.FlexEvent;
import mx.rpc.http.HTTPService;
private var svc:HTTPService=new HTTPService();
private function loadVal(evt:FlexEvent):void
{
svc.url="test2.txt";
svc.resultFormat="flashvars";//结果格式-值对
svc.addEventListener(ResultEvent.RESULT,handleResult);
svc.send();
}
private function handleResult(evt:ResultEvent):void
{
lbl.text=svc.lastResult.lbl;
combox.dataProvider=svc.lastResult.jobs.split(",");
}
]]>
</mx:Script>
<mx:HBox>
<mx:Label id="lbl"/>
<mx:ComboBox id="combox"/>
</mx:HBox>
</mx:Application>
<!--使用HTTPService类,加载URL编码数据-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="loadVal(event);">
<mx:Style>
Application{fontSize:12px;color:#ff0000;}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.events.FlexEvent;
import mx.rpc.http.HTTPService;
private var svc:HTTPService=new HTTPService();
private function loadVal(evt:FlexEvent):void
{
svc.url="test2.txt";
svc.resultFormat="flashvars";//结果格式-值对
svc.addEventListener(ResultEvent.RESULT,handleResult);
svc.send();
}
private function handleResult(evt:ResultEvent):void
{
lbl.text=svc.lastResult.lbl;
combox.dataProvider=svc.lastResult.jobs.split(",");
}
]]>
</mx:Script>
<mx:HBox>
<mx:Label id="lbl"/>
<mx:ComboBox id="combox"/>
</mx:HBox>
</mx:Application>
结果:
6.使用URLLoader类加载值对数据:
<?xml version="1.0" encoding="utf-8"?>
<!--使用URLLoader类,加载URL编码数据-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="loadVal(event);">
<mx:Style>
Application{font-size:14px;color:green;}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
import flash.net.URLLoader;
private var loader:URLLoader=new URLLoader();
private function loadVal(evt:FlexEvent):void
{
loader.addEventListener(Event.COMPLETE,handleComplete);
loader.load(new URLRequest("test2.txt"));
}
private function handleComplete(evt:Event):void
{
var value:URLVariables=new URLVariables(evt.target.data);
lbl.text=value.lbl;
combox.dataProvider=value.jobs.split(",");
}
]]>
</mx:Script>
<mx:HBox>
<mx:Label id="lbl"/>
<mx:ComboBox id="combox"/>
</mx:HBox>
</mx:Application>
<!--使用URLLoader类,加载URL编码数据-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="loadVal(event);">
<mx:Style>
Application{font-size:14px;color:green;}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
import flash.net.URLLoader;
private var loader:URLLoader=new URLLoader();
private function loadVal(evt:FlexEvent):void
{
loader.addEventListener(Event.COMPLETE,handleComplete);
loader.load(new URLRequest("test2.txt"));
}
private function handleComplete(evt:Event):void
{
var value:URLVariables=new URLVariables(evt.target.data);
lbl.text=value.lbl;
combox.dataProvider=value.jobs.split(",");
}
]]>
</mx:Script>
<mx:HBox>
<mx:Label id="lbl"/>
<mx:ComboBox id="combox"/>
</mx:HBox>
</mx:Application>
结果:
相关文章