6 Replies Latest reply on Sep 5, 2008 9:23 AM by greg h_

    Converting HTTPService to AS3

    SiHoop Level 1
      One of the things I don't like about Flex is having to flip between xmxl and .as files as I am working through the logic of my code. As a result, I want to convert all of my HTTPService tags to AS3. I don't see any point in writing the HTTPService in mxml as there is no visual element to it. So my question is this: Can I simply use a loadVars object to manage my calls and returns to and from php (does it even exist in AS3?), or should I be using the HTTPService in AS3. If I should be using the HTTPService in AS3, how would I write the following?

      Many thanks for looking.
        • 1. Re: Converting HTTPService to AS3
          Level 7

          "SiHoop" <webforumsuser@macromedia.com> wrote in message
          news:g9q5e8$nhr$1@forums.macromedia.com...
          > One of the things I don't like about Flex is having to flip between xmxl
          > and
          > .as files as I am working through the logic of my code. As a result, I
          > want to
          > convert all of my HTTPService tags to AS3. I don't see any point in
          > writing the
          > HTTPService in mxml as there is no visual element to it. So my question is
          > this: Can I simply use a loadVars object to manage my calls and returns to
          > and
          > from php (does it even exist in AS3?), or should I be using the
          > HTTPService in
          > AS3. If I should be using the HTTPService in AS3, how would I write the
          > following?
          >
          > Many thanks for looking.
          >
          > <mx:HTTPService id="login_user" result="checkLogin(event)" method="GET"
          > useProxy="false" url=" http://localhost/admin_php/login.php">
          > <mx:request xmlns="">
          > <username>
          > {username}
          > </username>
          > <password>
          > {password}
          > </password>
          > </mx:request>
          > </mx:HTTPService>

          There's a sort of crap example at the bottom of this page that might point
          you in the right direction
          http://www.adobe.com/livedocs/flex/3/html/help.html?content=data_access_2.html
          Here's another example that's about something else but you may find useful
          http://thanksmister.com/?p=43


          • 2. Re: Converting HTTPService to AS3
            greg h_
            Hi SiHoop,

            The last section on the doc page at the following link is entitled "Calling HTTP services in ActionScript"
            http://livedocs.adobe.com/flex/3/html/data_access_2.html

            I also have an issue switching between mxml to as. And once I get going I tend to prefer just staying in ActionScript.

            One great thing about using microarchitectures like Cairngorm or PureMVC is that outside of the view, they generally mandate using ActionScript. (Although, I do have Service classes where I still use MXML on the RPC classes. 6 of 1, half dozen of another. Just so long as it works ;-)

            Please post back regarding whether the above answers your question, and if you find it helpful :-)

            Best regards,

            g
            • 3. Re: Converting HTTPService to AS3
              greg h_ Level 1
              P.S.

              The Flex 2 docs on " Programming ActionScript 3.0" say:
              ActionScript 3.0 has replaced the LoadVars class with URLLoader and URLVariables classes.

              The Flex 3 docs on "Using the URLLoader and URLVariables classes" are here:
              http://livedocs.adobe.com/flex/3/html/17_Networking_and_communications_3.html

              The docs there comment:
              The new event-handling model is a significant improvement over the ActionScript 2.0 support for the LoadVars.onData, LoadVars.onHTTPStatus, and LoadVars.onLoad event handlers because it allows you to handle errors and events more efficiently.

              Again, hth,

              g
              • 4. Re: Converting HTTPService to AS3
                SiHoop Level 1
                greg h_,

                Thanks for the two replies. I took a look at the page your recommended Text I am frustrated with the explanation for two reasons. First, when I use the sample code to simply fire a php page I get an error message back. It seems that I am unable to even hit the file which I know is running properly when I test it via a web browser. Second, I do not know how to aggregate the data that is to be sent. The example indicates that I am supposed to use the following, but I do not know how to send through my data as 'parameters:Object', in part because my data are stored in text variables:

                public function useHttpService(parameters:Object):void {

                I wish the example provided by adobe had been a direct translation of the sample code given in the section titled MXML code, including an explanation of how to include the username and emailaddress included in the <mx:request> tag. So my question to you is: How do I specify the variables that I am to send?

                Thanks also for the explanation of how to load data from external documents in AS3-- very important! Below is the code I used to try to fire the php page:
                • 5. Re: Converting HTTPService to AS3
                  greg h_ Level 1
                  Hi SiHoop,

                  In the following documentation page, please see under the heading "Explicit parameter passing with HTTPService components":
                  http://livedocs.adobe.com/flex/3/html/data_access_5.html

                  Basically there are 3 ways to pass params to the server via HTTPService:
                  1) Bindings (in link above, see heading "Parameter binding with HTTPService components"). This option mainly applies when instantiating HTTPService via MXML (i.e. <mx:HTTPService>), not when instantiating via ActionScript (as you are doing).
                  2) In send() by position relative to how defined on serverside method (e.g. myService.send( myText.text, myText2.text )
                  3) In send() via object w/ properties that match param names defined on serverside method, (e.g.
                  var params:Object = new Object();
                  params.param1 = myText.text;
                  params.param2 = myText2.text;
                  myService.send(params);

                  For option 3 above, please note that in the doc page that I had referenced my first reply, where their "parameters:Object" was being passed in on the signature of the method definition of useHttpService(parameters:Object), the object parameters would have been built in the calling code. Unfortunately, the current version of that documentation does not show any calling code. (Just before I had replied last night, Amy had fairly posted that this was "a sort of crap example".) So, depending on your requirement you can build the object elsewhere and pass it in, or you can build the object immediately before executing send(params).

                  btw, Following is a "Flex quick start guide for PHP developers" complete with sample files. It does not cover executing HTTPService via ActionScript. However, if you have not yet reviewed it, perhaps it may help you with some working examples of Flex working with PHP backends.

                  Please post back regarding whether the above answers your question, and if you find it helpful :-)

                  Best regards,

                  g
                  • 6. Re: Converting HTTPService to AS3
                    greg h_ Level 1
                    P.S.

                    In my last post I forgot to add the following link where I had written "btw, Following is a "Flex quick start guide for PHP developers" ... "
                    http://www.adobe.com/devnet/flex/articles/php_getstarted.html

                    Even better may be the Flex 3 "Getting Started" examples that also provide complete with sample files:
                    http://learn.adobe.com/wiki/display/Flex/2b.+Code+Files
                    This link is into "Part II. Exchanging Data", "2b. Code Files"

                    The related page to "Download preconfigured Flex Builder projects" is here:
                    http://learn.adobe.com/wiki/display/Flex/Download+Projects

                    Again, the examples I am providing these links into do not cover executing HTTPService via ActionScript. They do, however, provide working examples of Flex working with PHP backends.

                    Please post back regarding whether the above answers your question, and if you find it helpful :-)

                    Best regards,

                    g