Flex与Java低数据量通信息二:URLLoader及非视图组件的事件发布订阅

该实例使用URLLoader向服务器获取XML和txt(properties格式数据)数据,由于获取数据由URLLoader完成,我将些操作单独放到另一个as文件中,故与视图组件分开,那么如何通告视图组件已完成数据加载功能呢?我在自己的as文件中自定义了一个事件,当URLLoader完成数据加载时,发布自定义的事件并将数据随事件一起分布,再在视图组件中订阅自定义的事件。

 

一、先来认识一下URLLoader:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
				layout="absolute" fontSize="12" backgroundGradientAlphas="[1.0, 1.0]" 
				backgroundGradientColors="[#FB7305, #FAE605]" width="621" height="385">	
	<mx:Script>
		<![CDATA[
			import com.mengya.URLLoaderTxt;
			
			internal function doTXT():void{
				var urlLoaderTxt:URLLoaderTxt = new URLLoaderTxt();
			}			
		]]>
	</mx:Script>
	<mx:Button x="49" y="40" label="测试URLLoader,读取文本" width="193" height="30" click="doTXT();"/>
</mx:Application>

 URLLoaderTxt 类如下:

package com.mengya{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	public class URLLoaderTxt extends Sprite {
		public function URLLoaderTxt(){
			var loader:URLLoader = new URLLoader();
			//订阅URLLoader的事件
			configListeners(loader);
			var request:URLRequest = new URLRequest("myTxt.txt");
			loader.load(request);
		}
		private function configListeners(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE,loaderComplete);
			dispatcher.addEventListener(Event.OPEN,openLoader);
		}
		private function loaderComplete(event:Event):void {
			var urlLoader:URLLoader = URLLoader(event.target);
			trace(urlLoader.data)
			var urlVariables:URLVariables = new URLVariables(urlLoader.data);
			trace(urlVariables.msg);
		}
		private function openLoader(event:Event):void{
			trace("打开连接");
		}
	}
}

 

对URLLoader功能有初步的认识以后完成以上的功能,如下:

mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
				layout="absolute" fontSize="12" backgroundGradientAlphas="[1.0, 1.0]" 
				backgroundGradientColors="[#FB7305, #FAE605]" width="621" height="385"
				applicationComplete="initApp();">	
	<mx:Script>
		<![CDATA[
			import com.mengya.LoadFinishEvent;
			import com.mengya.URLLoaderXML;
			
			[Bindable]
			private var dp:XMLList;
						
			internal function initApp():void{
				//添加自己定义的数据加载完成事件
				URLLoaderXML._eventDispatcher.addEventListener(LoadFinishEvent.LOADFINISHED,onLoadFinished);				
			}
			internal function doXML():void{
				var urlLoaderXML:URLLoaderXML = new URLLoaderXML();
			}
			internal function onLoadFinished(event:LoadFinishEvent):void{
				dp = new XMLList(event.data);
			}

		]]>
	</mx:Script>
	<mx:Button x="49" y="88" label="测试URLLoader,读取XML" width="193" height="30" click="doXML();"/>
	<mx:DataGrid x="49" y="147" width="529" height="193" dataProvider="{dp}">
		<mx:columns>
			<mx:DataGridColumn headerText="姓名" dataField="name" fontFamily="宋体" resizable="true"/>
			<mx:DataGridColumn headerText="电话" dataField="phone" fontFamily="宋体" resizable="true"/>
			<mx:DataGridColumn headerText="邮箱" dataField="email" fontFamily="宋体" resizable="true"/>
		</mx:columns>
	</mx:DataGrid>
</mx:Application>

 自定义事件类LoadFinishEvent如下:

package com.mengya{
	import flash.events.Event;
	//自己定义一个事件,用于发布数据加载完成,将加载完数据随事件一起发布
	public class LoadFinishEvent extends Event{
		public static const LOADFINISHED:String = "Loadfinished";
		public var data:String;
		public function LoadFinishEvent(type:String,
										data:String,
										bubbles:Boolean=false,
										cancelable:Boolean=false){
			this.data = data;
			super(type,bubbles,cancelable);
		}
	} 
}

 

完成发布自定义的事件的类如下:

package com.mengya
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	//这是一个非视图组件,在向服务器获取数据后发布一个自定义的事件,并将数据随着事件一起发布
	public class URLLoaderXML extends Sprite{
		
		public static var _eventDispatcher:EventDispatcher = new EventDispatcher();
				
		public function URLLoaderXML(){
			var loader:URLLoader = new URLLoader();
			//配置对URLoader的事件订阅
			configureListeners(loader);
			var request:URLRequest = new URLRequest("XMLFile.xml");
			try{
				//获取指定的XML数据
				loader.load(request);
			}catch(error:Error){
				trace(error.message);
			}		
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void{
			//订阅了URLLoader的Event.COMPLETE事件,表示加载完成
			dispatcher.addEventListener(Event.COMPLETE,completeHandler);
		}
		
		public function completeHandler(event:Event):void{
			var urlloader:URLLoader = URLLoader(event.target);
			//urlloader.data已包含加载来的数据
			trace(urlloader.data);
			//发布自定义的事件,并将所加载完成的数据随事件一起发布,用于在视图组件订阅捕获
			var evt:LoadFinishEvent = new LoadFinishEvent(LoadFinishEvent.LOADFINISHED,
														  urlloader.data);	
			_eventDispatcher.dispatchEvent(evt);
		}
	}
}

 

你可能感兴趣的:(java,.net,Flex,Flash,Adobe)