如何从AS3中的URLLoader的完整事件处理程序获取URLRequest的数据

时间:2011-03-17 21:30:43

标签: actionscript-3 url

我有一个消息队列,我想发送到一个URL,我想在我确定它们已成功发送后才从该队列中删除消息。要做到这一点,我需要在COMPLETE事件中知道URLLoader确切地发送了什么数据,以便我可以从队列中删除正确的消息。

那就是我有这样的东西。

var urlRequest:URLRequest = new URLRequest(targetUrl);  
var urlLoader:URLLoader = new URLLoader();  
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;  
urlRequest.data = "test";  

urlRequest.method = URLRequestMethod.POST;  
urlLoader.addEventListener(Event.COMPLETE, handleComplete);  
urlLoader.load(urlRequest);  

然后我的handleComplete函数是这样的:

public function handleComplete(e:Event):void{  

   //How do I trace the urlRequest.data for this event, whats below does not work.  
   //Because the target of the event is the URLLoader, not the URLRequest.  

   trace(e.target.data);  
}

1 个答案:

答案 0 :(得分:1)

要回答关于如何获取URLRequest对象的问题,您只需创建自己的自定义URLLoader类来存储URLRequest对象。以下是此示例:

CustomURLLoader.as:

package
{
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class CustomURLLoader extends URLLoader
    {
        private var _urlRequest:URLRequest;

        public function get urlRequest():URLRequest
        {
            return _urlRequest;

        }// end function

        public function CustomURLLoader(urlRequest:URLRequest)
        {
            super(urlRequest);
            _urlRequest = urlRequest;

        }// end function

    }// end class

}// end package

Main.as(文档类):

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;

    public class Main extends Sprite
    {
        public function Main()
        {
            if(stage) init()
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        public function init(e:Event = null):void
        {
            var urlRequest:URLRequest = new URLRequest("file.php");
            urlRequest.data = "test"; 
            urlRequest.method = URLRequestMethod.POST;  
            var customURLLoader:CustomURLLoader = new CustomURLLoader(urlRequest);
            customURLLoader.addEventListener(Event.COMPLETE, onCustomURLLoaderComplete);

        }// end function

        private function onCustomURLLoaderComplete(e:Event):void
        {
            var customURLLoader:CustomURLLoader = e.target as CustomURLLoader;
            trace(customURLLoader.urlRequest.data); // output: test

        }// end function

    }// end class

}// end package