9 Replies Latest reply on Nov 29, 2007 1:49 PM by peterent

    URLLoader - how to wait until URL is loaded and then contine...

    Misenko
      Hello,

      I have litle problem because I don't know how to load external XML file, wait when loading is completed and after that continue with next code.

      Consider following situation. I have a function in actionscript, let's say, handleXML()

      function handleXML() {

      someURLRequest = new URLRequest("some XML file");
      someURLLoader.addEventListener("complete","LoadCompleted");
      someURLLoader.load(someURLRequest );
      Alert.show("Print Second")
      }

      function LoadCompleted(event:Event) {
      someXML = XML(someURLLoader.data)
      Alert.Show("Print first")
      }

      The problem is, when someURLLoader.load is called, Alert window with Print second is displayed and then "Print first" alert is displayed because flex perform action behind someURLLoader.load and then perform function LoadCompleted(). I would like that first, when someURLLoader.load is called, flex wait until external XML is loaded, display "Prinf first" alert AND THEN continue with code and displat "Print second" alert.

      It is possible to do it someway??

      thanks for help
        • 1. Re: URLLoader - how to wait until URL is loaded and then contine...
          peterent Level 2
          I would use HTTPService instead:

          srv = new HTTPService();
          srv.url = "some XML file";
          srv.resultFormat = "xml";
          srv.addEventListener( ResultEvent.RESULT, loadCompleted );
          srv.send();

          function loadCompleted( event:ResultEvent ) : void
          {
          someXML = XML(event.result);
          }

          I think you'd be better off.
          • 2. Re: URLLoader - how to wait until URL is loaded and then contine...
            Misenko Level 1
            Hi, thanks for answer, but I think it is the same:) So what happens if I change your code like this...

            srv = new HTTPService();
            srv.url = "some XML file";
            srv.resultFormat = "xml";
            srv.addEventListener( ResultEvent.RESULT, loadCompleted );
            srv.send();
            Alert.show("Print Second")

            function loadCompleted( event:ResultEvent ) : void
            {
            someXML = XML(event.result);
            Alert.show("Print First")
            }

            In this case, after calling srv.send(), next code in the fuction is performed, so the Alert "Print Second"" is dsplayed. After that, URL request is completed and "Print Second" appears. I think it is because data calls are asynchronous. Is it there a possibillity to wait after calling send() function, until the request is processed and then continue with code which the send() function was called from ? It means, first Alert.show("Print First") is executed and then Alert.show("Print Second").???
            • 3. Re: URLLoader - how to wait until URL is loaded and then contine...
              peterent Level 2
              It is not possible to wait. Network apps that make requests have no idea how long it will take to get the response back. Network latency, a busy server, etc. So it is impractical to wait.
              • 4. Re: URLLoader - how to wait until URL is loaded and then contine...
                Misenko Level 1
                well, but it is at least possible to add some loop after load() call which wait for example for 5 seconds whereas response is returned and stored in XML file? It is a problem because after load call I have some code which use data from XML variable... Thanks
                • 5. Re: URLLoader - how to wait until URL is loaded and then contine...
                  peterent Level 2
                  There is no way to know how long it will take to get the XML result - you should put the code that needs the XML into the result event handler (loadCompleted) function. A timer is too unreliable.
                  • 6. Re: URLLoader - how to wait until URL is loaded and then contine...
                    xrum Level 2
                    i just did the same thing. took me a while to figure it out.

                    what i do is lodad my xml, while it's loading i show a little timer cursor and disable cursors. when it's loaded i enable everything. and done!

                    look here
                    http://xrumxrum.com/blog/2007/11/29/requestwaitload/
                    • 7. URLLoader - how to wait until URL is loaded and then contine...
                      Misenko Level 1
                      Hmm, maybe you don't understand me well. So antother example. Look at this:
                      function someFunction() {

                      srv = new HTTPService();
                      srv.url = "some XML file";
                      srv.resultFormat = "xml";
                      srv.addEventListener( ResultEvent.RESULT, loadCompleted );
                      srv.send();

                      If(someXML.child(0) == "xxx") {
                      Alert.show("SomeText")
                      }
                      }

                      function loadCompleted( event:ResultEvent ) : void
                      {
                      someXML = XML(event.result);
                      }

                      This is the problem, because loadCompleted event runs when someFunction() ends. So always, as the program end, the someXML variable will be empty....and the Alert never be displayed even if someXML.child(0) value equals "xxx" in the external xml file....
                      • 8. URLLoader - how to wait until URL is loaded and then contine...
                        xrum Level 2
                        why don't you do it this way:


                        <mx:HTTPService id=”FEX” url=”someXML.xml” resultFormat=”e4x”
                        result=”quickResultHandler(event);” fault=”quickFaultHandler(event);”/>

                        so then your quick ResultHandler will run when the XML is loaded.
                        i'm pretty new to all thsi too, so there's a good chance that i'm just not understanding whats going on. sorry.

                        private function quickResultHandler(event:ResultEvent):void

                        {
                        FEX = event.result as XML;
                        }

                        // Fault handler - displays the error
                        private function quickFaultHandler(event:FaultEvent):void

                        {
                        Alert.show(event.fault.message, "Could not load FY xml");
                        }
                        • 9. Re: URLLoader - how to wait until URL is loaded and then contine...
                          peterent Level 2
                          There is no way to immediately get the result once send() has been invoked. You must wait for the result. That's true with any data service in Flex/Flash (including URLLoader).

                          Try this instead:
                          function someFunction() {

                          srv = new HTTPService();
                          srv.url = "some XML file";
                          srv.resultFormat = "xml";
                          srv.addEventListener( ResultEvent.RESULT, loadCompleted );
                          srv.send();

                          }

                          function loadCompleted( event:ResultEvent ) : void
                          {
                          someXML = XML(event.result);

                          If(someXML.child(0) == "xxx") {
                          Alert.show("SomeText")
                          }
                          }