Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to process the return string from a URLLoader.load call
    primarykey
    data
    text
    <p>I have the requirement in a Flex application to create &amp; populate an object from the return from a call to a URL. Here is what I need to be able to do:</p> <ol> <li>I have a class which communicates with the web server.</li> <li>I have a function in this class (called getPerson) which would return a Person object which is populated from the XML data returned from the web server.</li> </ol> <p>The problem I am running into (and it seems this is a very common problem, but I have not seen a solution which I can see would work) is that the load method of the URLLoader is asynchronous.</p> <p>I have an event listener firing on the Event.COMPLETE event which parses the XML and populates my object in the event handler, but how would I get this object back to my originating code in my application which originally called my getPerson function?</p> <p>So by the time the return from the server comes back, my method is finished and I cannot return my populated Person object.</p> <p>My question is how can I accomplish this? I am still fairly new to ActionScript and have been spinning my wheels for a day now on this.</p> <p>I've added some sample code which demonstrates the problem I am having - I have simplified what I am using:</p> <p><strong>MXML application file:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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)"&gt; &lt;fx:Script&gt; &lt;![CDATA[ import mx.events.FlexEvent; protected function application1_creationCompleteHandler(event:FlexEvent):void { var d:DAL = new DAL(); d.CreateNewPerson( "John Smith" ); } ]]&gt; &lt;/fx:Script&gt; &lt;fx:Declarations&gt; &lt;!-- Place non-visual elements (e.g., services, value objects) here --&gt; &lt;/fx:Declarations&gt; &lt;/s:Application&gt; </code></pre> <p><strong>DAL.cs file:</strong></p> <blockquote> <pre><code>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 = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;"; 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() } } } </code></pre> </blockquote> <p>What I need to do is somehow get the "obj" variable back out to my MXML application file so I can use it there.</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload