需要处理URLLoader.load调用的返回字符串



我在Flex应用程序中有创建&从对URL的调用返回中填充对象。以下是我需要做的事情:

  1. 我有一个与网络服务器通信的类
  2. 我在这个类中有一个函数(称为getPerson),它将返回一个Person对象,该对象是由web服务器返回的XML数据填充的

我遇到的问题(这似乎是一个非常常见的问题,但我还没有看到一个可行的解决方案)是URLLoader的加载方法是异步的。

我有一个事件侦听器在event.COMPLETE事件上启动,它解析XML并在事件处理程序中填充我的对象,但我如何将此对象返回到我的应用程序中的原始代码中,该应用程序最初调用了我的getPerson函数?

因此,当服务器返回时,我的方法已经完成,无法返回填充的Person对象。

我的问题是我如何才能做到这一点?我对ActionScript还是相当陌生的,并且已经为此旋转了一天。

我添加了一些示例代码来演示我遇到的问题——我简化了我正在使用的内容:

MXML应用程序文件:

<?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文件:

package
{
  import flash.events.Event;
  import flash.net.URLLoader;
  import flash.net.URLRequest;
  import flash.net.URLRequestMethod;
  import mx.controls.Alert;
  public class DAL
  {
      public function DAL()
      {
      }
      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);
          var request:URLRequest = new URLRequest( "http://www.cnn.com" );
          request.method = URLRequestMethod.POST;
          request.data = strXML;
          loader.load(request);
      }
      private function onPostComplete( evt:Event ):void
      {
          //Process returned string
          //Here is where I need to return my object
                  var obj:Object = new Object()
      }
  }
}

我需要做的是以某种方式将"obj"变量返回到我的MXML应用程序文件中,以便在那里使用它。

提前感谢!

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

您的主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);
        }

    }
}

希望这能帮助

最新更新