7 Replies Latest reply on Jan 22, 2009 8:22 PM by Newsgroup_User

    Use mx:HTTPService in ActionScript

    2009 Matt Level 1
      I want to create an singleton event logger in ActionScript that uses mx:HTTPService. This logger only makes a simple
      PHP calls with an event code. In Flex, I can just call the singleton AS when I need to log an event in server.
      How to use HTTPService in AS? I'm not using cairngorm.
        • 1. Re: Use mx:HTTPService in ActionScript
          Level 7

          "2009 Matt" <webforumsuser@macromedia.com> wrote in message
          news:gl66pt$aio$1@forums.macromedia.com...
          >I want to create an singleton event logger in ActionScript that uses
          > mx:HTTPService. This logger only makes a simple
          > PHP calls with an event code. In Flex, I can just call the singleton AS
          > when I
          > need to log an event in server.
          > How to use HTTPService in AS? I'm not using cairngorm.

          You can see an example of using RemoteObject like this here:
          http://flexdiary.blogspot.com/2009/01/lazy-loading-tree-example-file-posted.html


          • 2. Re: Use mx:HTTPService in ActionScript
            2009 Matt Level 1
            I still want to use HTTPService in AS.

            In my old MXML file, I have
            <mx:HTTPService id="logger" url=" http://www.XYZ.com/php/log.php" useProxy="false" method="POST">
            <mx:request xmlns="">
            <logType>CONTACT_US</logType>
            </mx:request>
            </mx:HTTPService>
            It works fine. I want it be replaced with a Singleton EventLogger.as:

            package com.XYZ.actionscripts.util {
            import mx.rpc.http.HTTPService;

            [Bindable]
            public class EventLogger {
            static private var instance:EventLogger;
            private var httpService:HTTPService;
            public function log(log_code:XML):void {
            this.httpService.send(log_code);
            return;
            }
            static public function getInstance():EventLogger {
            if (instance == null){
            instance = new EventLogger2()
            }
            return instance;
            }
            public function EventLogger(){
            httpService = new HTTPService();
            httpService.url=" http://www.XYZ.com/php/log.php";
            httpService.useProxy=false;
            httpService.method="POST";
            httpService.contentType="application/xml";
            }
            }
            }

            In my new MXML file, I use:

            <mx:Script><![CDATA[
            import com.bao.actionscripts.util.EventLogger;
            public function log():void {
            var logCode:XML = new XML("<logType>CONTACT_US</logType>");
            EventLogger2.getInstance().log(logCode);
            return;
            }
            ]]></mx:Script>

            When I ran the code, I did not get any error message, but the php did not get the parameter ("<logType>CONTACT_US</logType>").

            Any ideas?
            • 3. Re: Use mx:HTTPService in ActionScript
              Level 7

              "2009 Matt" <webforumsuser@macromedia.com> wrote in message
              news:gl89n6$q6$1@forums.macromedia.com...
              >I still want to use HTTPService in AS.
              >
              > In my old MXML file, I have
              > <mx:HTTPService id="logger" url=" http://www.XYZ.com/php/log.php"
              > useProxy="false" method="POST">
              > <mx:request xmlns="">
              > <logType>CONTACT_US</logType>
              > </mx:request>
              > </mx:HTTPService>
              > It works fine. I want it be replaced with a Singleton EventLogger.as:
              >
              > package com.XYZ.actionscripts.util {
              > import mx.rpc.http.HTTPService;
              >
              > [Bindable]
              > public class EventLogger {
              > static private var instance:EventLogger;
              > private var httpService:HTTPService;
              > public function log(log_code:XML):void {
              > this.httpService.send(log_code);
              > return;
              > }
              > static public function getInstance():EventLogger {
              > if (instance == null){
              > instance = new EventLogger2()
              > }
              > return instance;
              > }
              > public function EventLogger(){
              > httpService = new HTTPService();
              > httpService.url=" http://www.XYZ.com/php/log.php";
              > httpService.useProxy=false;
              > httpService.method="POST";
              > httpService.contentType="application/xml";
              > }
              > }
              > }
              >
              > In my new MXML file, I use:
              >
              > <mx:Script><![CDATA[
              > import com.bao.actionscripts.util.EventLogger;
              > public function log():void {
              > var logCode:XML = new XML("<logType>CONTACT_US</logType>");
              > EventLogger2.getInstance().log(logCode);
              > return;
              > }
              > ]]></mx:Script>
              >
              > When I ran the code, I did not get any error message, but the php did not
              > get
              > the parameter ("<logType>CONTACT_US</logType>").
              >
              > Any ideas?

              Your problem is that you have a normal constructor, which a Singleton
              shouldn't have, and your getInstance() method is returning an instance of a
              _different_ class.

              That aside, I'd write it instead to just have static methods:

              package com.XYZ.actionscripts.util {
              [Bindable]
              public class EventLogger {

              private static var httpService:HTTPService = initService();
              public static function log(log_code:XML, resultHandler:Function = null,
              faultHandler:Function = null):void {
              var token:AsyncToken = httpService.send(log_code);
              var resHandler:Function;
              var fHandler:Function;
              //if the developer didn't specify fault and result handlers, use
              internal version
              if (!resultHandler) {
              resHandler = internalResultHandler;
              } else {
              resHandler = resultHandler;
              }
              if (!faultHandler) {
              fHandler= internalFaultHandler;
              } else {
              fHandler= faultHandler;
              }
              token.addResponder(new Responder(resHandler, fHandler);
              }
              private static function initService():HTTPService {
              var service:HTTPService = new HTTPService();
              service.url=" http://www.XYZ.com/php/log.php";
              service.useProxy=false;
              service.method="POST";
              service.contentType="application/xml";
              return service;
              }
              private static function internalResultHandler(e:ResultEvent):void {
              trace('completed successfully');
              }
              private static function internalFaultHandler(e:FaultEvent):void {
              trace('oops. there was a problem');
              }
              }
              }


              You'd call it like this:

              public function log():void {
              var logCode:XML = new XML("<logType>CONTACT_US</logType>");
              EventLogger.log(logCode);
              return;//don't need this line...there's nothing else in the function, so
              it will return
              }


              • 4. Re: Use mx:HTTPService in ActionScript
                2009 Matt Level 1
                With my original code, I can see the call to php script was made successfully and a row of event data was inserted in the EVENT DB table. So my singleton class dose create an instance of the correct class. The only problem I have is that the log_code is not submitted to the php correctly so it is missing in the DB table.

                I do not have the handlers for ResultEvent and FaultEvent, but they are not related to my problem here.
                • 5. Re: Use mx:HTTPService in ActionScript
                  Level 7

                  "2009 Matt" <webforumsuser@macromedia.com> wrote in message
                  news:glb0j1$cvb$1@forums.macromedia.com...
                  > With my original code, I can see the call to php script was made
                  > successfully
                  > and a row of event data was inserted in the EVENT DB table. So my
                  > singleton
                  > class dose create an instance of the correct class. The only problem I
                  > have is
                  > that the log_code is not submitted to the php correctly so it is missing
                  > in the
                  > DB table.
                  >
                  > I do not have the handlers for ResultEvent and FaultEvent, but they are
                  > not
                  > related to my problem here.

                  I did some other things that will help the way your class works. First,
                  your implementation of Singleton is problematic, and doesn't prevent
                  instantiation. Second, it only creates one HTTPService, rather than a new
                  instance every time you call the function. This will help with memory
                  management, along with just being more elegant and less wasteful. By making
                  the functions and variables static, I also simplified the process of calling
                  the function, since you don't need to stick the getInstance() in there.

                  I just put in the handler functions to show you how to do it :).

                  Is your script expecting to receive GET or POST data? I don't believe you
                  were setting the method on the HTTPService, so make sure you're expecting
                  $_GET parameters.

                  HTH;

                  Amy


                  • 6. Re: Use mx:HTTPService in ActionScript
                    2009 Matt Level 1
                    I have changed the Singleton HTTPService:

                    package com.XYZ.actionscripts.util {
                    import mx.rpc.http.HTTPService;
                    import mx.rpc.AsyncToken;

                    [Bindable]
                    public class EventLogger2 {
                    static private var instance:EventLogger2;
                    static private var httpService:HTTPService;

                    public function log(log_code:XML):void {
                    var ret:AsyncToken = httpService.send(log_code);
                    }

                    static public function getInstance():EventLogger2 {
                    if (instance == null){
                    instance = new EventLogger2()
                    EventLogger2.init();
                    }
                    return instance;
                    }

                    static private function init():void {
                    httpService = new HTTPService();
                    httpService.url=" http://www.XYZ.com/php/log.php";
                    httpService.useProxy=false;
                    httpService.method="POST";
                    httpService.contentType="application/xml";
                    }
                    }
                    }

                    I think it is better than the first version. I did set the method as "POST". I have no control on the php script.
                    But it works well with HTTPService in MXML file with "POST" request. The basic problem is how to create HTTPService in a AS and use it to send XML data. This is how I use the Singleton:

                    <mx:Script><![CDATA[
                    ...
                    public function log():void {
                    var logCode:XML = new XML("<logType>HOME2</logType>");
                    EventLogger2.getInstance().log(logCode);
                    //logger.send();
                    return;
                    }
                    ]]></mx:Script>

                    I'll add the handlers later after I resolve this problem. Thanks for your suggestions!
                    • 7. Re: Use mx:HTTPService in ActionScript
                      Level 7

                      "2009 Matt" <webforumsuser@macromedia.com> wrote in message
                      news:glbb70$oo1$1@forums.macromedia.com...
                      >I have changed the Singleton HTTPService:
                      >
                      > package com.XYZ.actionscripts.util {
                      > import mx.rpc.http.HTTPService;
                      > import mx.rpc.AsyncToken;
                      >
                      > [Bindable]
                      > public class EventLogger2 {
                      > static private var instance:EventLogger2;
                      > static private var httpService:HTTPService;
                      >
                      > public function log(log_code:XML):void {
                      > var ret:AsyncToken = httpService.send(log_code);
                      > }
                      >
                      > static public function getInstance():EventLogger2 {
                      > if (instance == null){
                      > instance = new EventLogger2()
                      > EventLogger2.init();
                      > }
                      > return instance;
                      > }
                      >
                      > static private function init():void {
                      > httpService = new HTTPService();
                      > httpService.url=" http://www.XYZ.com/php/log.php";
                      > httpService.useProxy=false;
                      > httpService.method="POST";
                      > httpService.contentType="application/xml";
                      > }
                      > }
                      > }
                      >
                      > I think it is better than the first version. I did set the method as
                      > "POST". I
                      > have no control on the php script.
                      > But it works well with HTTPService in MXML file with "POST" request. The
                      > basic
                      > problem is how to create HTTPService in a AS and use it to send XML data.
                      > This
                      > is how I use the Singleton:
                      >
                      > <mx:Script><![CDATA[
                      > ...
                      > public function log():void {
                      > var logCode:XML = new XML("<logType>HOME2</logType>");
                      > EventLogger2.getInstance().log(logCode);
                      > //logger.send();
                      > return;
                      > }
                      > ]]></mx:Script>
                      >
                      > I'll add the handlers later after I resolve this problem. Thanks for your
                      > suggestions!

                      You may want to try calling toXMLString() on the XML before sending it.
                      Another thing you may want to try setting the request object separately
                      rather than putting it in the send parameter.

                      Also, you might want to add on a fault handler just to make sure you are
                      not getting an error.

                      HTH;

                      Amy