13 Replies Latest reply on Oct 23, 2008 10:44 AM by alice_data

    HTTPService Request and Actionscript Issues

    alice_data Level 1
      Hi,

      I don't know if anyone on the list could tell me what I am trying to do here is possible. The scenario is that I have a HTTPService here that is functionally working great, but I want to do the rest of the processing internally, instead of having PHP (the one from the HTTPService do the job)

      I wanted to do a function call to the HTTPService and then handle the variables I retrieve from the PHP, which currently I simply use something like {simulation.lastResult.service.number}. If I want to extract this value directly and do some calculation manipulation with it in Actionscript, how can I extract the value of the above without rewriting my HTTPService? (Or, whatever is most efficient?)

      Note: I currently use PHP to do the same thing, but I would like to do this internally without hoarding my remote server.

      Any help is appreciated.

      Here is the HTTPService I have:
        • 1. Re: HTTPService Request and Actionscript Issues
          Gregory Lafrance Level 6
          Anytime you receive a result from an HTTPService you can process that result in the result event handler, and so in that handler you could put the returned data in an XMLListCollection, ArrayCollection, Array, Object, etc.

          So without changing your HTTPService you could possibly make minor modifications to your result handler and be in a position to do what you desire.
          • 2. Re: HTTPService Request and Actionscript Issues
            alice_data Level 1
            Hi,

            Now I think I am kind of confused, but I think I will try again here to explain further on what I tried to do.
            Currently, whatever I have generated from this HTTPService is something like {simulation.lastResult.simulation.status} so that I can extract the value and have it output to the screen.

            Say that I have two values to output on the screen from the HTTPService based on my previous post, {simulation.lastResult.simulation.status} and {simulation.lastResult.simulation.number} , and I want to say output a new value of {simulation.lastResult.simulation.status} - {simulation.lastResult.simulation.number} instead of having this processed in my PHP file as in the HTTPService, but in the Actionscript. What are the possible measures I have to take without using URLLoader()?

            Thanks again for your help.

            Alice


            • 3. Re: HTTPService Request and Actionscript Issues
              m_hartnett Level 3
              You want to do as Greg stated. Your HTTPService request can define a result and fault method handlers such as:

              <mx:HTTPService id="simulation"
              url=" http://localhost/myfile.php"
              useProxy="false"
              showBusyCursor="true"
              result="someResultFunctionName"
              fault="someErrorFunctionName:
              method="POST">
              <mx:request xmlns="">
              <market>{market.text}</market>
              <pop>{pop.text}</pop>
              <length>{length.text}</length>
              </mx:request>
              </mx:HTTPService>

              The returned data is available in the result handler method. In this method you can manipulate the data anyway you would like. The result handler method gets executed automatically when the HTTPService call is completed and it is passed the data returned from the server.

              For example, here is the call:
              <mx:HTTPService
              id="loginService"
              url="{model.initServerName}"
              resultFormat="xml"
              result="loginResultHandler(event);"
              fault="loginFaultHandler(event);"/>

              Here is the method that handles the result.
              private function loginResultHandler(e:ResultEvent) : void {

              your return data is in the variable e.result.
              }
              • 4. Re: HTTPService Request and Actionscript Issues
                Gregory Lafrance Level 6
                Yeah, most of the time you want to use a result handler, not lastResult when processing your HTTPService results.
                • 5. Re: HTTPService Request and Actionscript Issues
                  alice_data Level 1
                  Thanks for the suggestions, guys. I have revised my code to do most of what I meant to do, except that I have a final step where I hope that the code would help me process some of the values without the help of the additional processing using PHP through HTTPService.

                  Right now the result refuses to calculate using the existing declared values I have using Actionscript. The other lines that are supposed to print out variables do get printed out, except for the one in the following that is being processed in the Actionscript snippet. Could anyone please help me with what I might have done wrong here?

                  Thanks for your help.

                  The code snippet is provided below:
                  • 6. Re: HTTPService Request and Actionscript Issues
                    Gregory Lafrance Level 6
                    What is happening, an error?

                    Could it be that the data is being returned as strings and you need to do something like:

                    var remaining= int(total_buy)-int(total_sell);
                    • 7. Re: HTTPService Request and Actionscript Issues
                      alice_data Level 1
                      Hi,

                      So you are suggesting that the syntax is accurate? I tried what you suggested, but it still did not give me anything.
                      To prove that the function is correctly called, the snippet <mx:Label text="{total_buy}" x="175" y="325" color="#0000FF" fontWeight="bold"/> is bringing back the accurate value.

                      However, I get no error messages even when I run the debug mode for what I have with what you have suggested, and therefore the place where it is supposed to have some number from <mx:Label text="{remaining}" x="575" y="325" color="#0000FF" fontWeight="bold"/> gives a blank.

                      Could I be doing something wrong here?

                      Thanks for your help.

                      Alice
                      • 8. Re: HTTPService Request and Actionscript Issues
                        m_hartnett Level 3
                        I notice that you have a couple HTTPService calls defined here. One in MXML and one in actionscript. It looks like you are using the actionscript one to make the call. Is that correct?

                        In your result handler method, the 'remaining' variable is a local variable to the method. Do you want to define that variable at the object level so that your window can use the variable as well?
                        • 9. HTTPService Request and Actionscript Issues
                          alice_data Level 1
                          Hi,
                          I think what you said here about what I tried to do is accurate. I have some values I would like to extract from the httpservice request, and have them output to the page, plus some values I would like to additional manipulate using Actionscript processing to print them on the screen.

                          The remaining variable is one of them that I would like to process based on the values output from the HTTPService. I have modified my code so that it processes the function declaration from the Actionscript, <mx:Button label="Submit" id="mySubmitButton" click="useHttpService(Object)"/>, but now I get errors near the labels where I intend to display the variables I have declared, such as total_buy, and I get this error message at the following line:

                          <mx:Label text="{total_buy}" x="175" y="325" color="#0000FF" fontWeight="bold"/>
                          1120: Access of undefined property total_buy

                          Have I done something wrong here?

                          Thanks for your help.

                          Alice
                          • 10. Re: HTTPService Request and Actionscript Issues
                            m_hartnett Level 3
                            So when you execute this code you are calling simulation.send(parameters) (the MXML version) and never executing useHttpService? In that case you could eliminate the actionscript code. Either call would be fine. However I notice that you are binding the MXML url to the service.destination (url="{service.destination}") so if you get rid of the actionscript code you need to make sure the MXML url is set to http://myfile/simulator.php. However executing either method would be fine.


                            The httpResult method will receive the data from PHP. The form of the data should be in XML because the resultFormat of the MXML service call is set to that. However your resultFormat can return other types than XML. Take a look at the api doc for the HTTPService.

                            Once that service returns it executes your method

                            public function httpResult(event:ResultEvent):void {
                            var result:Object = event.result;
                            var total_buy= event.result.root.total_buy;
                            var total_sell=event.result.root.total_sell;
                            var remaining= total_buy-total_sell;
                            }

                            This method looks fine and if you are correctly getting the values for the total_buy and total_sell then you know that the data is comming back from PHP fine. If you are trying to bind the 'remaining' variable to some field, it is defined as a local variable to this method and will not be bindable to something outside this method. You should either make the remaining variable and instance variable or set the text item directly like textRemaining.text = total_buy-total_sell.

                            <mx:Label id="textRemaining" x="655" y="325" color="#0000FF" fontWeight="bold"/>

                            The last possible issue I see here is that the variables dont specify a type. Change them to (give them an int type)

                            var total_buy:int= event.result.root.total_buy;
                            var total_sell:int=event.result.root.total_sell;
                            var remaining:int= total_buy-total_sell;

                            • 11. Re: HTTPService Request and Actionscript Issues
                              alice_data Level 1
                              Hi,

                              Thanks for the explanations. I have changed the submit process to <mx:Button label="Submit" id="mySubmitButton" click="simulation.send(parameters)"/>, and removed the entire Actionscript portion for useHTTPService().

                              What astounds me is that even when I did define the three variables and attempted to have the result give me the output of remaining variable, it never did. I don't get any errors from type errors at the moment after putting in the type of each variable with your fixings. Your reply shows that I should treat the remaining variable as its own by defining it as text, but I get an error that says Access of undefined property when I tried to do this.

                              I hope I am getting closer to getting the remaining variable output to the screen. Everything else is working at the moment.

                              Thanks again for your help.

                              Alice
                              • 12. Re: HTTPService Request and Actionscript Issues
                                m_hartnett Level 3
                                Try this example. It uses a XML file from my site as the source data and does not use PHP for any processing but it gives the basics.

                                <?xml version="1.0" encoding="utf-8"?>
                                <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                                layout="absolute">
                                <mx:Script>
                                <![CDATA[
                                import mx.controls.Alert;
                                import mx.rpc.events.FaultEvent;
                                import mx.rpc.events.ResultEvent;

                                //define your variables at instance level and make sure they are bindable
                                [Bindable]private var totalBuy:int = 0;
                                [Bindable]private var totalSell:int=0;
                                [Bindable]private var remaining:int=0;

                                private function resultHandler(e:ResultEvent) : void {
                                //This returns XML data your data may be different.
                                //You will see that it is XML returned. Your code will change here
                                //depending on what is being returned.
                                var xmlLst : XMLList = new XML(e.result).children();
                                Alert.show(xmlLst.toXMLString(),"Return Values");
                                totalBuy = xmlLst[0];
                                totalSell = xmlLst[1];
                                remaining = totalBuy - totalSell;
                                }

                                private function faultHandler(e:FaultEvent) : void {
                                Alert.show(e.message.toString(),"Error Occured");
                                }
                                private function runService() : void {
                                dataService.send();
                                }



                                ]]>
                                </mx:Script>
                                <mx:HTTPService
                                id="dataService"
                                url=" http://www.siteorderly.com/xml/costData.xml"
                                resultFormat="xml"
                                result="resultHandler(event);"
                                fault="faultHandler(event);"/>

                                <mx:Label x="26" y="27" text="Total Buy:"/>
                                <mx:Label x="26" y="53" text="Total Sell:"/>
                                <mx:Text x="96" y="27" id="totalBuyText" text="{totalBuy}"/>
                                <mx:Text x="96" y="53" id="totalSellText" text="{totalSell}"/>
                                <mx:Label x="26" y="87" text="Remaining:"/>
                                <mx:Text x="96" y="87" id="remainingText" text="{remaining}"/>
                                <mx:Button x="96" y="126" label="Run" click="runService()"/>


                                </mx:Application>
                                • 13. Re: HTTPService Request and Actionscript Issues
                                  alice_data Level 1
                                  Hi,

                                  Thanks for the reply and the example. This really helped, and after slight modification of my original code, everything is now working.

                                  Alice