Actionscript 3 需要处理URLLoader.load调用的返回字符串
在Flex应用程序中,我需要从调用URL的返回中创建并填充一个对象。以下是我需要能够做到的:Actionscript 3 需要处理URLLoader.load调用的返回字符串,actionscript-3,apache-flex,Actionscript 3,Apache Flex,在Flex应用程序中,我需要从调用URL的返回中创建并填充一个对象。以下是我需要能够做到的: 我有一个与web服务器通信的类 我在这个类中有一个函数(称为getPerson),它将返回一个Person对象,该对象由web服务器返回的XML数据填充 我遇到的问题(这似乎是一个非常常见的问题,但我还没有看到一个我认为可行的解决方案)是URLLoader的加载方法是异步的 我在event.COMPLETE事件上启动了一个事件侦听器,该事件解析XML并在事件处理程序中填充我的对象,但是我如何将该对象返回
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var d:DAL = new DAL();
d.CreateNewPerson( "John Smith" );
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</s:Application>
DAL.cs文件:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var d:DAL = new DAL();
d.CreateNewPerson( "John Smith" );
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</s:Application>
包
{
导入flash.events.Event;
导入flash.net.urloader;
导入flash.net.URLRequest;
导入flash.net.URLRequestMethod;
导入mx.controls.Alert;
公共类DAL
{
公共职能()
{
}
公共函数CreateNewPerson(名称:字符串):void
{
var strXML:String=“”;
变量加载器:URLLoader=新的URLLoader();
loader.addEventListener(Event.COMPLETE、onPostComplete);
变量请求:URLRequest=新的URLRequest(“http://www.cnn.com" );
request.method=URLRequestMethod.POST;
request.data=strXML;
加载(请求);
}
完成后的私有函数(evt:事件):无效
{
//进程返回的字符串
//这是我需要归还物品的地方
var obj:Object=新对象()
}
}
}
我需要做的是以某种方式将“obj”变量返回到我的MXML应用程序文件中,以便在那里使用它
提前谢谢 您的主MXML文件
<fx:Script>
<![CDATA[
import flash.events.Event;
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var d:DAL = new DAL();
d.addEventListener(Event.COMPLETE, onLoadComplete);
d.CreateNewPerson( "John Smith" );
}
private function onLoadComplete(e:Event):void
{
trace("DATA LOADED")
}
]]>
</fx:Script>
还有你的DAL.as
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import mx.controls.Alert;
/**
* ...
* @author Jeet Chauhan
*/
public class DAL implements IEventDispatcher
{
private var dispatcher:IEventDispatcher;
public function DAL() {
dispatcher = new EventDispatcher();
}
public function CreateNewPerson( Name:String ):void {
/*var strXML:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onPostComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, onPostComplete);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onPostComplete);
var request:URLRequest = new URLRequest( "http://www.cnn.com" );
request.method = URLRequestMethod.POST;
request.data = strXML;
loader.load(request);*/
// let assume data loaded and onPostComplete called
onPostComplete(new Event(Event.COMPLETE));
}
private function onPostComplete(evt:Event):void {
//Process returned string
//Here is where I need to return my object
var obj:Object = new Object()
dispatchEvent(evt);
}
/* INTERFACE flash.events.IEventDispatcher */
public function dispatchEvent(event:Event):Boolean
{
return dispatcher.dispatchEvent(event);
}
public function hasEventListener(type:String):Boolean
{
return dispatcher.hasEventListener(type);
}
public function willTrigger(type:String):Boolean
{
return dispatcher.willTrigger(type);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
{
dispatcher.removeEventListener(type, listener, useCapture);
}
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
{
dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
}
}
包
{
导入flash.events.Event;
导入flash.events.EventDispatcher;
导入flash.events.IEventDispatcher;
导入flash.events.IOErrorEvent;
导入flash.events.SecurityErrorEvent;
导入flash.net.urloader;
导入flash.net.URLRequest;
导入flash.net.URLRequestMethod;
导入mx.controls.Alert;
/**
* ...
*@作者吉特·乔汉
*/
公共类DAL实现IEventDispatcher
{
专用var调度器:IEventDispatcher;
公共职能(){
dispatcher=新的EventDispatcher();
}
公共函数CreateNewPerson(名称:字符串):void{
/*var strXML:String=“”;
变量加载器:URLLoader=新的URLLoader();
loader.addEventListener(Event.COMPLETE、onPostComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR,onPostComplete);
loader.addEventListener(SecurityErrorEvent.SECURITY\u错误,onPostComplete);
变量请求:URLRequest=新的URLRequest(“http://www.cnn.com" );
request.method=URLRequestMethod.POST;
request.data=strXML;
加载(请求)*/
//假设已加载数据并调用onPostComplete
onPostComplete(新事件(Event.COMPLETE));
}
完成后的私有函数(evt:事件):无效{
//进程返回的字符串
//这是我需要归还物品的地方
var obj:Object=新对象()
调度事件(evt);
}
/*接口flash.events.IEventDispatcher*/
公共函数dispatchEvent(事件:事件):布尔值
{
返回dispatcher.DispatcheEvent(事件);
}
公共函数hasEventListener(类型:String):布尔值
{
返回dispatcher.hasEventListener(类型);
}
公共函数willTrigger(类型:String):布尔值
{
返回dispatcher.willTrigger(类型);
}
公共函数removeEventListener(类型:String,侦听器:function,useCapture:Boolean=false):void
{
dispatcher.removeEventListener(类型、侦听器、useCapture);
}
公共函数addEventListener(类型:String,侦听器:function,useCapture:Boolean=false,优先级:int=0,useWeakReference:Boolean=false):void
{
dispatcher.addEventListener(类型、侦听器、useCapture、优先级、useWeakReference);
}
}
}
希望对您的mxml应用程序文件提供以下帮助:
var d:DAL = new DAL();
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
d.CreateNewPerson( "John Smith" );
d.addEventListener('PersonCreated', personCreated);
}
private function personCreated(evt:Event) :void
{
var obj:Object = new Object();
obj = d.ojectToBeReturned;
// obj will contain the object from your class...
}
在DAL类上,声明对象变量并创建getter/setter函数,即
private var _myObjectToBeReturned:Object;
public function get ojectToBeReturned() :Object
{
return _myObjectToBeReturned;
}
还有你的方法
private function onPostComplete( evt:Event ):void
{
//Here is where I need to return my object
_myObjectToBeReturned = new Object();
// Perform the process for the object.
// Call the event from your parent.
this.dispatchEvent(new Event('PersonCreated'));
}
如果你发布一些代码会有帮助。另一方面,为什么不将源代码中的代码移动到Event.COMPLETE Event?嗨,Jeetendra-你能通过修改我添加到原始问题中的示例代码来说明你的意思吗?我明白你的意思,但我不知道如何在AS3。。。谢谢