9 Replies Latest reply: Nov 20, 2007 6:58 AM by PremierITA RSS

    HTTPService in ActionScript

    PremierITA
      I've created this class

      quote:


      package services
      {
      import mx.collections.ArrayCollection;
      import mx.rpc.http.HTTPService;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.events.ResultEvent;

      public class RSSService implements IService
      {
      private var _feeds:ArrayCollection;
      private var _titleChannel:String;

      private var _rssService:HTTPService;

      public function RSSService(url:String) {
      _rssService= new HTTPService();
      _rssService.url= url;
      _rssService.useProxy= false;
      }

      public function send():void {
      _rssService.send();
      _rssService.addEventListener(ResultEvent.RESULT, resultHandler);
      _rssService.addEventListener(FaultEvent.FAULT, faultHandler);
      }

      private function resultHandler(event:ResultEvent):void {
      _feeds= event.result.rss.channel.item;
      _titleChannel= event.result.rss.channel.title;
      }

      private function faultHandler(event:FaultEvent):void {

      }

      public function getResult():Object {
      if (_feeds == null) _rssService.send();

      var result:Object= new Object();
      result.feeds= _feeds;
      result.titleChannel= _titleChannel;
      return result;
      }

      }
      }



      and i use it in this way
      quote:


      [Bindable]
      public var feed:ArrayCollection;

      [Bindable]
      public var titleChannel:String;
      private function onConfirm():void {
      var service:RSSService= new RSSService(rssUrl.selectedItem.data);
      var obj:Object= service.getResult();
      titleChannel= obj.titleChannel;
      feed= obj.feeds;
      currentState= 'reader';
      }


      in the Application file.
      When i launch the application, the HTTPService object doesn't call resultHandler(event:ResultEvent) method, so the _feeds Array is empty.

      Why?

      I need tutorials, where i can found they?

      Byr
        • 1. Re: HTTPService in ActionScript
          PremierITA Community Member
          Is there someone here?

          Who want learn and develop in flex with me please?

          Bye
          • 2. Re: HTTPService in ActionScript
            atta707 Community Member
            Are you sure your URL is getting called at all? What if you add some alert message in the fault hanlder?

            ATTA
            • 3. Re: HTTPService in ActionScript
              kbala_net Community Member
              Hi,

              it seems ti should work. thing is...you do not call getResult() immediately before resultHandler fires. So, make sure resultHandler fires and then call getResult().

              Thanks

              KBala
              • 4. Re: HTTPService in ActionScript
                PremierITA Community Member
                quote:

                Originally posted by: kbala.net@gmail.com
                Hi,

                it seems ti should work. thing is...you do not call getResult() immediately before resultHandler fires. So, make sure resultHandler fires and then call getResult().

                Thanks

                KBala


                Yes, but how i can solve?
                I'm using this code now and it works correctly
                quote:


                var service:RSSService= new RSSService(rssUrl.selectedItem.data);
                service.send();
                setTimeout(
                function send():void {
                var obj:Object= service.getResult();
                titleChannel= obj.titleChannel;
                feed= obj.feeds;
                },
                1000
                );



                But this code is inside the caller function.

                How i can solve in my class ?
                • 5. Re: HTTPService in ActionScript
                  kbala_net Community Member
                  just dispatch an event in this function

                  xxxxx your code xxxxx
                  private function resultHandler(event:ResultEvent):void {
                  _feeds= event.result.rss.channel.item;
                  _titleChannel= event.result.rss.channel.title;

                  ********dispatch the event here**************
                  }


                  and, capture the event where you use the class's instance.

                  thanks

                  KBala
                  • 6. Re: HTTPService in ActionScript
                    PremierITA Community Member
                    What it means?

                    In resultHandler I set the class property and after you must invoke getResult gives the object with the class property.
                    • 7. Re: HTTPService in ActionScript
                      PremierITA Community Member
                      Must I use _rssService.dispatchEvent method? How?
                      • 8. HTTPService in ActionScript
                        kbala_net Community Member
                        hi,

                        i rewirte your code like this

                        class
                        ------------------

                        package services
                        {
                        import flash.events.EventDispatcher;
                        import mx.collections.ArrayCollection;
                        import mx.rpc.http.HTTPService;
                        import mx.rpc.events.ResultEvent;
                        import mx.rpc.events.FaultEvent;
                        import flash.events.Event;

                        [Event(name="result",type="mx.rpc.events.ResultEvent")]
                        public class RssService extends EventDispatcher
                        {
                        private var _feeds:ArrayCollection;
                        private var _titleChannel:String;

                        private var _rssService:HTTPService;

                        public function RssService(url:String)
                        {
                        super();
                        _rssService= new HTTPService();
                        _rssService.url= url;
                        _rssService.useProxy= false;
                        }

                        public function send():void
                        {
                        _rssService.send();
                        _rssService.addEventListener(ResultEvent.RESULT, resultHandler);
                        _rssService.addEventListener(FaultEvent.FAULT, faultHandler);
                        }

                        private function resultHandler(event:ResultEvent):void
                        {
                        _feeds= event.result.rss.channel.item;
                        _titleChannel= event.result.rss.channel.title;
                        var result:Object= new Object();
                        result.feeds= _feeds;
                        result.titleChannel= _titleChannel;
                        var evt:ResultEvent = new ResultEvent("result",false,true,result);
                        dispatchEvent(evt);
                        }

                        private function faultHandler(event:FaultEvent):void
                        {

                        }


                        }
                        }



                        Implementation
                        ----------------------------

                        import mx.rpc.events.ResultEvent;
                        import services.RssService;
                        import mx.core.SoundAsset;
                        private var r:RssService;

                        private function init():void
                        {
                        r = new RssService("url here");
                        r.addEventListener(ResultEvent.RESULT,resultHandler)
                        r.send()

                        }

                        private function resultHandler(event:ResultEvent):void
                        {
                        // use event.result here
                        }


                        this will be solve your issue, good luck!

                        Thanks

                        KBala
                        • 9. Re: HTTPService in ActionScript
                          PremierITA Community Member
                          Very well.

                          THANK YOU VERY MUCH.