开发者

How do I get the data of the URLRequest from the Complete Event handler for a URLLoader in AS3

I have a queue of messages that I would like to send to a URL and I would like to remove messages from that queue only after I am sure they have been successfully sent. To do this I need to know in the COMPLETE event for the URLLoader exactly what data was sent so that I can remove the correct message from the queue.

That is if I ha开发者_如何学编程ve something like this.

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);  

And then my handleComplete function is like this:

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);  
}


To answer your question of how to get the URLRequest object you can simply create your own custom URLLoader class that stores the URLRequest object. The following is an example of this:

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(document class):

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜