ActionScript中的HTTP请求与外部通信

一、导航至URL
1.flash程序中最普通的HTTP请求是使用URLRequest类和navigateToURL()方法进行URL导航。
   1)创建URLRequest对象,填充url
   2)navigateToURL()进行访问,是在新选项卡打开还是在当前选项卡打开(_blank 、_self)
2.如果本地swf文件想要访问网络资源,需要设置flash player设置管理器,将运行的swf文件添加到其可信区域。

二、加载外部资源

1.加载外部文本文件,文本文件中可以夹杂着图片(html<img>标签之类),用URLRequest,URLLoad,和Textfield三个类
2.加载外部css文件,URLRequest,URLLoad、Textfield和styleSheet类,styleSheet通过parseCSS分析完加载的css文件后,,
可以给加载下来的外部文本文件添加样式。

三、错误处理

下面使用flash cs5写的一些actionscript代码,希望对大家有帮助

import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;

var imageLoad:Loader = new Loader();
imageLoad.load(new URLRequest("http://www.focusonmedia.com/vqs/as3/cheese_and_crackers.jpg"));//加载外部图片
imageLoad.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
imageLoad.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
imageLoad.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
addChild(imageLoad);

function progressHandler(evt:ProgressEvent):void{
	var percentage:Number = Math.round((evt.bytesLoaded/evt.bytesTotal)*100);
	trace(percentage);
}

function completeHandler(evt:Event):void{
	removeChild(imageLoad);
}
function ioErrorHandler(evt:IOErrorEvent):void{
	trace("加载出错");
}



意思是一样的,这些代码也可以搬到flex中

发布了36 篇原创文章 · 获赞 3 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/jinxiumeihappy/article/details/14169719
今日推荐