5 Replies Latest reply on Oct 31, 2007 9:30 AM by ntsiii

    A loop of HTTPService

    estacado1
      Hello there, a newbie here. I would like to know how do I make a loop with httpservice calls inside. I have an array called storyID, let's say:
      storyID = [4542,2354,2354,1234,7653];
      StoryID isn't always 5 elements long, it varies.

      The httpservice call url is like:
      h ttp://api.webservice.com/article/ storyID
      where storyID is one of the elements of the named array.

      So, I want to make a loop that goes through all the elements of storyID array and make an httpservice call on each iteration with the corresponding storyID.

      Using the repeater isn't working for me. I think the only way is to make the call in actionscript. Can anybody give an example, please. Thank you.
        • 1. Re: A loop of HTTPService
          atta707 Level 2
          I don't know if that would be most efficient way of getting data from remote server, but anyhow:

          <HTTPService id="s1" result....
          for each(var id:number in storyIds) {
          s1.send(id);
          }

          is it possible to send all these stories at once and get the data about all of them from the server in one request? if so, i think that should be preferred way.

          ATTA
          • 2. A loop of HTTPService
            estacado1 Level 1
            Here's how I did it:


            private function getStoryDetailsXML():void{

            for (var j:uint = 0; j < storyIDArray.length; j++){

            var httpStoryDetails:HTTPService = new HTTPService();

            httpStoryDetails.url = "h ttp://api.websercies.com/story/"+storyIDArray[j]+"?";

            httpStoryDetails.resultFormat = "e4x";

            httpStoryDetails.addEventListener("result", httpStoryDetailsHandler);

            httpStoryDetails.addEventListener("fault", httpFaultHandler);

            httpStoryDetails.send();


            function httpStoryDetailsHandler(event:Object):void{

            trace("j = " + j);

            myXML.story[j].@title = httpStoryDetails[j].lastResult.story.title.text();
            }

            }

            }


            The problem with this is that by the time httpStoryDetailsHandler gets called, the value of j will already be equal to the length if the storyIDArray (5 in this case). I need httpStoryDetailsHandler to be called for every value of j from 0 to 4. How do I go about this?
            • 3. Re: A loop of HTTPService
              mae@ccg Level 1
              I think you are close. A few suggestions

              1. Move the code to create the httpservice out of the loop. All you are changing is the parameter. Better yet, create the httpservice in mxml.
              2. As atta707 suggested, just call that same httpservice with a different parameter
              3. in your result handler stuff the result into an array collection or xmlcollection.
              oh, and most importantly
              4. Set the httpService mode to concurrent, since you are going to be calling them essentially at the same time

              Hope that helps.
              • 4. Re: A loop of HTTPService
                estacado1 Level 1
                Okay, finally got it working. I couldn't use s1.send(id); because the api I'm using requires the id to be part of the url itself, not as a parameter. Example:
                url="h ttp://api.webservice.com/article/storyID?appkey=myApp"
                and not:
                url="h ttp://api.webservice.com/article?story=storyID,appkey=myApp"

                I don't know whether it can be done with s1.send(id) format or not, but if it can, I still don't know how to do it.
                But anyway, I found a very helpful page on the livedocs.


                With the example on the livedoc page as a guide, I managed to come up with this:

                <mx:HTTPService
                id="httpStoryDetails"
                resultFormat="e4x"
                result="httpStoryDetailsHandler(event);"
                fault="httpFaultHandler(event);"
                showBusyCursor="true"
                />


                private function getStoryDetailsXML():void{
                for (var j:uint = 0; j < storyIDArray.length; j++){
                httpStoryDetails.url = "h ttp://api.webservice.com/article/"+storyIDArray[j]+"?appkey=myApp";
                var httpStoryDetailsCall:Object = httpStoryDetails.send();
                httpStoryDetailsCall.marker = "call"+j;
                }
                }


                private function httpStoryDetailsHandler(event:Object):void{
                var httpStoryDetailsCall:Object = event.token;
                for (var j:uint = 0; j < storyIDArray.length; j++){
                if (httpStoryDetailsCall.marker == ("call"+j)){
                myXML.story[j].@title = httpStoryDetails.lastResult.story.title.text();
                }
                }
                }


                That's all. I hope this helps other people encountering this problem. And by the way, it's really hard to show readable code on this forum. Adobe should do something about it.
                • 5. Re: A loop of HTTPService
                  ntsiii Level 3
                  I agree with the code formatting problem. I am sure they are working on it.

                  Two points:
                  First, send returns an "AsyncToken" so it would be better to write:
                  var httpStoryDetailsCall:AsyncToken = ...

                  Do this to send "GET" (the default method) variables in the request object:
                  var oRequest:Object = {appkey:"myApp"};
                  var httpStoryDetailsCall:AsyncToken = httpStoryDetails.send(oRequest);

                  You will still need to build the url dynamically, though, so what you have is fine if you are happy with it.
                  Tracy