2 Replies Latest reply on Jan 16, 2008 9:23 AM by ctaffe

    Returning HTTPResults from .as file

    ctaffe Level 1

      I'm trying to make a public function that will allow the return of my HTTPService results to the component that made the call. My main problem right now is the EventListener doesnt trip until after the return is already attempted.

      Basically within the individual mxml component I want to make a call something like:
      prodData = DataRetriever("Product", "ID12345");
      mfgrData = DataRetriever("Manufacturer", "Widget Company");

      The .as piece that contains the DataRetriever function currently reads:


      // ActionScript file

      import mx.controls.Alert;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.http.HTTPService;

      public var result:Object = new Object();

      public function DataRetriever(String:dataType, String:dataItem):Object {

      var params:Object = new Object() ;

      //set values for PHP script
      params.type = dataType;
      params.item = dataItem;

      var service:HTTPService = new HTTPService();

      service.url = " http://xxx.xxx.xxx.xxx/flex/return_data.php";

      service.addEventListener("result", httpResult);
      service.addEventListener("fault", httpFault);



      return result;

      } //end function DataRetriever

      public function httpResult(event:ResultEvent):void {

      result = event.result;
      result = new XML(result);


      public function httpFault(event:FaultEvent):void {

      var faultstring:String = event.fault.faultString;



      The actual PHP call isn't important as it does properly return an XML formated stream. As I said above the problem is that it makes the return call with empty data before the httpResult handler is called. If I run it twice it will give me the data held in result from the previous call but not the current one. As I'm fairly new to flex I kind of went about this how I would handle it in PHP and that is probably where I am running into problems. Is there any way to pause on the returning the result value until the HTTPService returns a value? While I could probably use a single public variable to put the data in and not use a return on the function I'm afraid that I wouldn't be able to reuse the function for calling for different data sets within the same mxml component. I apologize if there are any syntax errors above that were thrown in during the cut/paste.

      Thanks for any help!
        • 1. Re: Returning HTTPResults from .as file
          JKohn99 Level 1
          All calls from the flash player out to a network service are asynchronous.

          So your result will only be populated when your result event handler is invoked. You have no control over this so you must use events
          to get a result back to your main application.


          1) make your dataRetriver an event dispatcher by extending it from EventDispatcher.

          2) in your main line register an event listener with your dataRetriever.
          like: myDR.addEventListener("myEvent",processData)

          3) in your result handler dispatch myEvent
          dispatchEvent(new Event("myEvent")

          4) in processData(event:Event):void
          myDr.getResult() // or some other method

          alternatively make a custom event to be dispatched that holds the data you want to send.

          Just sub class Event and add your own variables etc.
          • 2. Returning HTTPResults from .as file
            ctaffe Level 1
            Thank you JKohn.

            With the help of mhartnett on some event handling issues I was able to get this to work nicely now by creating a listner in my component file that was dispatched by my httpResult function. I added some logic to my new listener so it knew what kind of information it was getting back and to assign it to the proper variable.

            Thanks again