3 Replies Latest reply on Jul 10, 2007 6:21 PM by ntsiii

    HTTPService closure/callback

    jhapak
      I am trying to implement closure example I found on a website somewhere but my alert is not working. That means call.handler is not executing properly. Here is the code:

      public function getSomething(url:String):Boolean
      {
      var httpService:HTTPService = new HTTPService();
      var call:Object;
      httpService.url = url;
      httpService.send();

      call = httpService.send();
      call.handler = function (event:Event):void
      {
      Alert.show("I am here");
      }

      return true;
      }


      Can somebody tell me where is the error? I am new to actionscript and flex.
        • 1. Re: HTTPService closure/callback
          anil_parashar
          can you tell me why you are calling send method two times.
          call once and than check it.
          • 2. Re: HTTPService closure/callback
            Level 7
            Keep your HTTPService as a member variable, and, if you're using Flex 2,
            the call token that is returned from HTTPService.send() is now
            mx.rpc.AsyncToken which you can register listeners for specific events,
            such as result and fault events.
            • 3. Re: HTTPService closure/callback
              ntsiii Level 3
              here are som snippets that might help.
              Tracy

              Sample code using HTTPService, e4x, handler function to populate a list item.
              Also shows usage of AsyncToken.

              The DataGrid tag:
              <mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>


              The HTTPService tag:
              <mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)" fault="..../>

              Script block declaration:
              import mx.rpc.Events.ResultEvent;
              [Bindable]private var _xlcMyListData:XMLListCollection;

              Invoke send:
              var oRequest:Object = new Object();
              oRequest.Arg1 = "value1";
              var callToken:AsyncToken = service.send(oRequest);
              token.callId = "myQuery1";

              Result Handler function:
              private function onResult(oEvent:ResultEvent):void {
              var xmlResult:XML = XML(event.result); //converts result Object to XML. can also use "as" operator
              var xlMyListData:XMLList = xmlResult.myListData; //depends on xml format, is row data
              _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a collection
              trace(_xlcMyListData.toXMLString()); //so you can see exactly how to specify dataField or build labelFunction
              var callToken:AsyncToken = oEvent.token;
              var sCallId = callToken.callId; //"myQuery1"
              switch(sCallId) {
              case "myQuery1":
              doQuery2();
              break;
              ...
              }
              }//onResult