12 Replies Latest reply on Nov 2, 2009 10:00 AM by levancho

    HTTPService Issue

    Matt Le Fevre Level 4

      Right, this problem has be following me since i started flex. I've tried to get a solution before, but i think i can explain the problem i am experiencing better.

       

      I'm guessing it is something to do with one of the following:

       

      - The way i've written my PHP.

      - The way i handle my HTTPServices.

      - The setup of the server i'm hosting the files off.

       

      A brief description of my problem.

       

      My program loads, and an HTTPService is sent off to a PHP file. The PHP file outputs information, and then i retrieve the outputting information using LastResult or a Result event.

       

      for example:

       

       

           <mx:HTTPService id="doStuff" showBusyCursor="true" method="POST" result="readStuff(event)" url="http://localhost/stuff.php" useProxy="false"/>

       

           -->

       

           <?php

       

           print "<Hello>Testing</Hello>";

       

           ?>

       

           -->

       

          private function readStuff(evt:ResultEvent):void
           {

                var whatever:String = evt.result.Hello;
           }

       

       

       

      This code works absolutely fine. If i set the HTTPService to run on a button click, no problems at all. However, if the program is loading and use "doStuff.send()" somewhere in the init(), or creationcomplete, or anywhere that isn't manually set aside from running with the main program, it will work erratically.

       

      Sometimes a value is returned from the HTTPService, sometimes nothing is.

       

      Currently, i have got all the services that run like this to work by using manual 'SetTimeout()'. Obviously as this is a very messy way of doing it, i want to try and stick clear of this.

       

       

      I thought it may have been the cache interfering, so i binded a string value to the url in the HTTPService and appended '?' + random numbers to the end of it to rule out the cache. It wasn't, and the problem persists.

       

       

      Which leads me to the final conclusion that sometimes, Flex is simply refusing to send the HTTPService request, for a reason i don't know.

       

       

      Another example, i have a load/save utility within the program i'm working on. When you change to a different screen, a request is sent off and a list of files is returned (the list is in the 1000's)

       

       

      If the HTTPService is linked to the button click which changes screen, all the file details are returned, or sometimes nothing is returned.

       

       

      However, if i set the HTTPService up in something like this:

       

      setTimeout(refreshFileList, 1000);

       

       

      it'll work every time.

       

       

      Any help on this crippling issue would be vastly appreicated. I really am stumped by what is going on.

       

       

      Many thanks for your time.

        • 1. Re: HTTPService Issue
          Subeesh Arakkan Level 4

          Did you try it on applicationComplete event ?

          • 2. Re: HTTPService Issue
            Matt Le Fevre Level 4

            Subeesh Arakkan wrote:

             

            Did you try it on applicationComplete event ?

            yep

             

            tried it on applicationComplete, initialize and creationComplete.

             

             

            The current setup which allows it to work is by calling a function on creationComplete, which delays the requests.

            • 3. Re: HTTPService Issue
              Matt Le Fevre Level 4

              Problem still persisting,

               

              anyone have any ideas?

              • 4. Re: HTTPService Issue
                Subeesh Arakkan Level 4

                are you getting any runtime error message?

                • 5. Re: HTTPService Issue
                  Matt Le Fevre Level 4

                  nope,

                   

                  the datagrid/list/combobox (wherever the data is going) simply doesn't populate.

                   

                   

                  i've tried putting an 'on fault' attribute into the HTTPService to try and catch when it fails, but it isn't triggered when the retrieval of data fails

                   

                   

                  It appears to be that the HTTPService simply isn't being carried out, and Flex randomly chooses to ignore it.

                  • 6. Re: HTTPService Issue
                    Subeesh Arakkan Level 4

                    Strange. I think the HTTPService is not completely instantiated even in the applicationComplete. Try doing it from actionscript. Something like this

                     

                    private var service:HTTPService 

                    public function useHttpService(parameters:Object):void {
                         service = new HTTPService();
                         service.destination = "sampleDestination";
                         service.method = "POST";
                         service.addEventListener("result", httpResult);
                         service.addEventListener("fault", httpFault);
                         service.send(parameters)
                    }
                    1 person found this helpful
                    • 7. Re: HTTPService Issue
                      Matt Le Fevre Level 4

                      implemented it, and i have exactly the same results :/

                       

                      first time the service is sent, data retrieved

                      second time, nothing

                      third time the service is sent, data retrieved

                      fourth time, nothing

                      fifth time the service is sent, data retrieved

                      etc....

                       

                       

                      This is really starting to annoy me

                      • 8. Re: HTTPService Issue
                        Jhon Carrillo

                        Hi All,

                         

                        I had the same issue. I think that it is generated by different moments of creation between the objects and the data loading. Maybe one of them is ready but the another one not. For example, I need to get the server date using a httpService when a titleWindow is called.

                        So,I created a httpService called postConfig and set "{ postConfig.lastResult.info.item.date}"   in the dateField text property.  I sent some posts  but sometimes the Server Date was not loaded from the httpService in the dateField.  No error is catched and the source code is very good.  The solution  below:

                         

                         

                                  1)  if you use AS3 to create the httpService  then always use bindable metadata tag. It allows to copy the changes of the httpService to the object

                         

                                     [Bindable]
                                     public var postConfig:HTTPService; 

                         

                                 2) Use  ResultEvent event to get exactly the  moment of the data return

                         

                                  public function postConfigResult(event:ResultEvent):void{
                                    try{   
                                        obj.dfdAgendaDate.text= postConfig.lastResult.info.item.date;
                                    }
                                        catch (err:Error){
                                             .....
                                       }

                         

                                    }

                                4) A null value not expected can produce an internal error that abort the next lines of code. If you httpService.send() command is wrote after this error then  It will be not executed.

                         

                                3) Finally, you need to send the post/get request using creationcomplete event in the container or in the object directly

                         

                        Take care,

                         

                        Jhon Carrillo

                        jhon.carrillo (@) gmail.com
                        Venezuela

                        • 9. Re: HTTPService Issue
                          levancho Level 3

                          It looks like you have a typical situation for race condition, sometimes your services return before some of your ui components are fully initialized, and sometimes they delay, whenever they delay you get result if they come back really quickly you dont .tricky thing is, even after ApplicationComplete not every component in Flex is initialized, or even worst they are not even created, that is done by design, (Lazy initialization)

                          qustion: where do you have this declared :

                           

                            <mx:HTTPService id="doStuff" showBusyCursor="true" method="POST" result="readStuff(event)" url="http://localhost/stuff.php" useProxy="false"/>

                           

                           

                          is it directly under main application or under some other visual component, that is possible hiddne, like a tabnavigations child ? etc ...

                           

                           

                          Have you tried using a ActionScript eqvivalent?

                           

                          like

                           

                          var params:Object = {};
                                         params["name"] = "value";
                                         var service : HTTPService = new HTTPService();
                                         service.destination = "someurl.php";
                                      service.method = "POST";
                                      service.addEventListener("result", httpResult);
                                      service.addEventListener("fault", httpFault);
                                      service.send(params);

                          this way it should definately work.

                           

                          another idea,speculating, I am not 100%  if it will  work, but you can try using CallLater , off  for example ApplicationComplete Event

                           

                           

                           

                          private function myApplicationCompleteListener(e:event) : void {
                          
                            callLater(myservice.send);
                          }
                          

                           

                           

                          hope that helps

                          Regards

                          Levan

                          • 10. Re: HTTPService Issue
                            Matt Le Fevre Level 4
                            qustion: where do you have this declared :

                             

                              <mx:HTTPService id="doStuff" showBusyCursor="true" method="POST" result="readStuff(event)" url="http://localhost/stuff.php" useProxy="false"/>

                             

                             

                            is it directly under main application or under some other visual component, that is possible hiddne, like a tabnavigations child ? etc ...

                             

                             

                            It's within the main application.

                             

                            Have you tried using a ActionScript eqvivalent?

                             

                            like

                             

                            var params:Object = {};
                                         &n bsp; params["name"] = "value";
                                         &n bsp; var service : HTTPService = new HTTPService();
                                         &n bsp; service.destination = "someurl.php";
                                        service.method = "POST";
                                        service.addEventListener("result", httpResult);
                                        service.addEventListener("fault", httpFault);
                                        service.send(params);

                            this way it should definately work.

                             

                            yeah i've tried doing it through ActionScript, same results.

                             

                            another idea,speculating, I am not 100%  if it will  work, but you can try using CallLater , off  for example ApplicationComplete Event

                             

                             

                             

                            private function myApplicationCompleteListener(e:event) : void {

                              callLater(myservice.send);
                            }

                             

                            yeah i've tried callLaters as well!

                             

                             

                             

                            I'm still heavily debating the idea that it's the server that it is hosted on is the issue.

                             

                            As it works fine on my localhost. Although that may just be a lag issue.

                            • 11. Re: HTTPService Issue
                              msakrejda Level 4

                              I'm still heavily debating the idea that it's the server that it is hosted on is the issue.

                               

                              As it works fine on my localhost. Although that may just be a lag issue.

                              Try Wireshark, Charles, or another program that will let you see bits on the wire to see what's actually coming back from your http call. This will help you narrow it down.

                              • 12. Re: HTTPService Issue
                                levancho Level 3

                                I highly  doubt it its server issue,

                                its very simple to rule that out, you can paste that url in browser and run it as many times as you like, if it always gives response thatnit can not possibly be a server.

                                 

                                you can also use tamperData firefox addon and do a request/response interseption to analyze what is exactly coming back from server before it reaches flex app.