OSMF的入门二(actionscript 网络通信API)

参考书籍:flash actionscript 3 殿堂之路 孙颖 p311 -320

数据格式:文本、值对、二进制

要建立一个成功能通信请求,通信请求对象必须知道足够的信息--------请求对象、方式、数据等,

主要包括:1、请求的有效URL地址  2、请求方式(get  or  post)

读取文本数据例子(带注释核心代码部分)与读取对值对格式的例子(不带注释)

	public class SampleXML  extends Sprite 
	{
	//	private var title_txt:TextField ;
		public function SampleXML():void
		{
			//var targetURL:String = "http://feeds.feedburner.com/Kingda";
		//	var targetURL:String = "test2.xml" ;
			var targetURL:String = "test1.txt" ;
			
			var reques:URLRequest = new URLRequest(targetURL);
			var variables:URLLoader;
			try
			{
				variables = new URLLoader();
			}catch(err:Error)
			{
				trace("err");
			};
			
			variables.dataFormat = URLLoaderDataFormat.VARIABLES ;
			variables.addEventListener(Event.COMPLETE ,completeHandle);
			variables.addEventListener(ProgressEvent.PROGRESS ,test);
			
			try
			{
				trace("loading ......");
				variables.load(reques);
			}
			catch(error:Error)
			{
				trace("uable to load url");
				
			}
			
	/*
			title_txt = new TextField();
			title_txt.autoSize = "left" ;
			addChild(title_txt);
			
		*/	
			
		}
		public function completeHandle(evn:Event):void
		{
		/*	trace(evn.target.data is String);
			var res:XML = new XML(evn.target.data);
			var titlelist:XMLList = res.channel.item.title;
			for each(var title:XML in titlelist)
			{
				title_txt.appendText("*" + title+"'\n");
				trace (title.toString());
			}
*/
			trace(evn.target.data);
			var loader:URLLoader = evn.target as URLLoader ;
			var i:String;
			for( i in loader.data)
			{
				trace(i+"\t:"+ loader.data[i] );
			}
			
			
		}
		public function test(evn:ProgressEvent):void
		{
					trace(evn.bytesTotal + "\t" +evn.bytesLoaded);
			//	trace(evn.);
			//	loader.
			
			if(evn.bytesLoaded >= 1024*1024)
			{
			trace("over");
			}
		}
	}

读取二进制格式数据的例子

		
	public class SampleBinary extends Sprite 
	{

		private var loader:Loader ;
		private var loader2:Loader;
		
		public function SampleBinary():void
		{
			//	var targetURL:String = "test2.xml" ;
			var targetURL:String = "D:/OSMF/OSMFPlayer/bin/OSMFPlayer.swf" ;
			
			var reques:URLRequest = new URLRequest(targetURL);
			var variables:URLLoader;
			
			variables = new URLLoader();
	
			
			variables.dataFormat = URLLoaderDataFormat.BINARY ;
			variables.addEventListener(Event.COMPLETE ,completeHandle);
			variables.addEventListener(ProgressEvent.PROGRESS ,test);
			
			try
			{
				trace("loading ......");
				variables.load(reques);
			}
			catch(error:Error)
			{
				trace("uable to load url"+error);
				
			}
			
		}
		public function completeHandle(evn:Event):void
		{
		
			trace("loaded");
			loader = new Loader();
			var content:ByteArray = evn.target.data as ByteArray ;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE ,convertHandle);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,test2);			
			loader.loadBytes(content);
			
		//	trace(content);
			
			loader2 = new Loader();
			loader2.contentLoaderInfo.addEventListener(Event.COMPLETE,convertHandle);
			loader2.loadBytes(content);
		
			
		}
		public function convertHandle(evn:Event):void
		{
			this.addChild(evn.target.loader as Loader);
			evn.target.loader.y = 50;  
			evn.target.loader.x =200*Math.random() ;		
			
		//	this.addChild(evn.target.loader2 as Loader);
		//	evn.target.loader2.y =100 ;
		//	evn.target.loader2.x = 50;
		}
		public function test(evn:ProgressEvent):void
		{
			//		trace(evn.bytesTotal + "\t" +evn.bytesLoaded);
			//	trace(evn.);
			//	loader.
			/*
			if(evn.bytesLoaded >= 1024*1024)
			{
			trace("over");
			}*/
			//	var loader:URLLoader = evn.target as URLLoader ;
			//trace(loader.data);
		}	
		public function test2(evn:ProgressEvent):void
		{
			trace(evn.bytesTotal + "\t" +evn.bytesLoaded);
			
			
		}
	}
















你可能感兴趣的:(OSMF的入门二(actionscript 网络通信API))