我有一个消息队列,我想发送到一个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);
}
答案 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