11 Replies Latest reply on Jun 11, 2010 10:37 AM by ~lordshark

    HTTPService


      Hey,
      I'm new to Flex.I have a problem with HTTPService.
      In my application am adding and getting tasks to/from database by using below two lines code.

      add.send(data);
      getTasks.send();

      And am assging result data to datagrid dynamically.when i call getTasks.send() method immedaitely some times it's giving result and
      sometimes not.That's why am using timer function to call that line.But in timer am using 2 seconds time.How to replace that 2 seconds time
      as httpservice response time.

      public function ShortTimer():void {
      // creates a new five-second Timer
      var minuteTimer:Timer = new Timer(2000,1);
      minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);

      // starts the timer ticking
      minuteTimer.start();
      }

      public function onTimerComplete(event:TimerEvent):void {
      getTasks.send();
      }

      Can u tel me "How to get the HTTPService Request time(while sending the request to Server)
      or Response time(while getting the data from the server)".My communication will not be best please dont mind.

      Thanks in advance,
      Nagarjuna.
        • 1. Re: HTTPService
          ntsiii Level 3
          Do not use Timer.

          Use a result event handler: Here are some code snippets:

          Tracy

          • 2. Re: HTTPService
            Level 1
            Hey ,
            Regarding HttpSerivce I posted my problem to flex forum.you replied to that with some sample code.Thanx for that.It's fine.I chnaged my code according to that.
            In my case am using popupwindow.And am assging data to datagrid dynamically from popupwindow.
            Here am sending two files.It's not entering into the switch block.

            Will you please have a look into these files and tell me what's the wrong with my code?

            finally my problem with httpservice is if server gives response immediately at all times no issues.But sometimes server takes more than 2 seconds due to internal issues at that time my data won't get refresh.Please see my code and give me solution.

            CreateGroup.mxml

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" initialize="initData();">

            <mx:Script>
            <![CDATA[
            import mx.controls.DataGrid;
            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;
            import mx.controls.ComboBox;
            import mx.managers.PopUpManager;
            import mx.utils.ArrayUtil;
            import mx.events.ValidationResultEvent;
            import mx.controls.Alert;

            [Bindable]
            public var groupsList:ArrayCollection = new ArrayCollection();
            //popup for creating groups
            private function createGroup():void {
            var creategroup:HttpServiceSync = HttpServiceSync(PopUpManager.createPopUp(this, HttpServiceSync, true));
            creategroup.dg = dg;
            }

            private function initData():void {
            getGroups.send();
            }

            private function resGetGroups(event:ResultEvent):void {
            if(event.result != null) {
            if(event.result.GroupList != null) {
            /* if (event.result.GroupList.GroupNames is ArrayCollection) {
            groupsList = event.result.GroupList.GroupNames;
            } else if (event.result.GroupList.GroupNames is ObjectProxy) {
            groupsList = new ArrayCollection(ArrayUtil.toArray(event.result.GroupList.GroupNames));
            } else {
            //nothing
            } */
            groupsList = event.result.GroupList.GroupNames as ArrayCollection;
            }
            }
            if(groupsList != null) {
            dg.dataProvider = groupsList;
            dg.validateNow();
            }
            }
            ]]>
            </mx:Script>

            <mx:HTTPService id="getGroups" url=" http://localhost:8080/test/GetGroup.jsp" result="resGetGroups(event);"/>

            <mx:VBox width="100%" height="100%">
            <mx:DataGrid id="dg" width="40%" height="60%" rowCount="5"
            verticalGridLines="false"
            dragEnabled="true"
            borderStyle="solid">
            <mx:columns>
            <mx:DataGridColumn id="group" dataField="groupname" headerText="Group"/>
            </mx:columns>
            </mx:DataGrid>
            <mx:Button label="Create Group" click="createGroup();"/>
            </mx:VBox>

            </mx:Application>


            HttpServiceSync.mxml

            <?xml version="1.0" encoding="utf-8"?>
            <mx:TitleWindow xmlns:mx=" http://www.adobe.com/2006/mxml"
            x="450" y="140"
            showCloseButton="true" close="closeWindow();"
            title="Create Group" borderColor="#3bb7af">

            <mx:Script>
            <![CDATA[
            import mx.controls.DataGrid;
            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.AsyncToken;
            import mx.controls.ComboBox;
            import mx.managers.PopUpManager;
            import mx.utils.ArrayUtil;
            import mx.events.ValidationResultEvent;
            import mx.controls.Alert;

            public var dg:DataGrid;
            [Bindable]
            private var groupsList:ArrayCollection = new ArrayCollection();

            private function addGroup():void {
            var result:ValidationResultEvent = gnValidator.validate();
            if (result.type == ValidationResultEvent.VALID) {
            closeWindow();
            Alert.show("invoking send");
            var callToken:AsyncToken = createGroup.send(addGroupData);
            callToken.callId = "myQuery1";

            /* createGroup.send(addGroupData); */

            } else {
            if(groupName.text != null)
            mx.controls.Alert.show("Please enter proper group name.", "Oops!", mx.controls.Alert.OK);
            else
            mx.controls.Alert.show("Group Name required.", "Oops!", mx.controls.Alert.OK);
            }
            }

            private function onResult(oEvent:ResultEvent):void {
            Alert.show("result method for inserting");
            var callToken:AsyncToken = oEvent.token;
            var sCallId:int = callToken.callId;
            switch(sCallId) {
            case "myQuery1":
            Alert.show("switch matched");
            getGroupList();
            break;
            }
            }

            private function getGroupList():void {
            Alert.show("getting result method");
            getGroups.send();
            }

            private function resGetGroups(event:ResultEvent):void {
            Alert.show("result groups method");
            if(event.result != null) {
            if(event.result.GroupList != null) {
            if (event.result.GroupList.GroupNames is ArrayCollection) {
            groupsList = event.result.GroupList.GroupNames;
            } else if (event.result.GroupList.GroupNames is ObjectProxy) {
            groupsList = new ArrayCollection(ArrayUtil.toArray(event.result.GroupList.GroupNames));
            } else {
            //nothing
            }
            }
            }
            if(groupsList != null) {
            dg.dataProvider = groupsList;
            dg.validateNow();
            }
            }

            private function closeWindow():void {
            PopUpManager.removePopUp(this);
            }
            ]]>
            </mx:Script>

            <mx:StringValidator id="gnValidator" source="{groupName}" property="text"
            trigger="{addGroupButton}" triggerEvent="click" requiredFieldError="Please enter proper Group Name"/>

            <mx:HTTPService id="createGroup" url=" http://localhost:8080/test/AddGroups.jsp" showBusyCursor="true" result="onResult(event);"/>
            <mx:HTTPService id="getGroups" url=" http://localhost:8080/test/GetGroup.jsp" showBusyCursor="true" result="resGetGroups(event);"/>

            <mx:Model id="addGroupData">
            <root>
            <groupName>{groupName.text}</groupName>
            </root>
            </mx:Model>

            <mx:VBox id="groupPanel" label="Create Groups" width="100%" height="100%">
            <mx:Form defaultButton="{addGroupButton}">
            <mx:FormItem label="Group Name:">
            <mx:TextInput id="groupName"/>
            </mx:FormItem>
            </mx:Form>
            <mx:HBox paddingLeft="40" paddingTop="15">
            <mx:Button id="addGroupButton" label="Create Group" click="addGroup();"/>
            <mx:Button id="cancel" label="Cancel" click="closeWindow();"/>
            </mx:HBox>
            </mx:VBox>

            </mx:TitleWindow>

            Thanks & Regards,
            Nagarjuna
            • 3. Re: HTTPService
              shailuvs
              Hi,

              I also have the same problem in my application.I am trying sample aplication using httpserivce to display product details in grid.If user wants to add new product info
              i am using popup window to add.For me also it's inserting into database fine.No issues from db side.Only thing I am struggling is ui.
              My grid is not refreshing everytime.If I add 100 times it is inserted into database 100 times.But grid will not refresh 100 times.
              Some times I need to refresh manually to see latest changes (newly inserted record).
              How to resolve this?
              anybody please help me......

              thanks
              shailaja
              • 4. Re: HTTPService
                gallaharsha
                Hi,
                I was facing a similar issue i.e need to update the grid without manual refresh. I got it solved. My approach was that I populated the whole grid again as and when i thought the data might have changed due to my action of adding task or deleting task.
                hope this helps..
                • 5. Re: HTTPService
                  shailuvs Level 1
                  Hi harsha,
                  ok that's fine from ui.
                  Another problem need to solve is..
                  when ever i add a product from popup...the below lines of code getting executed in my app......

                  add.send(productData);
                  getProducts.send();(this will call result handler method onResult(event))

                  private function onResult(event:ResultEvent):void {
                  prodcutsList = event.result.ProductInfo.ProductList as ArrayCollection;
                  datagrid.dataProvider = prodcutsList;
                  }

                  this is the code from popupwindow........
                  I used alert boxes to debug my application finally i found what's the problem.Initially prodcutsList variable length is 0.If i add a new product prodcutsList variable length will increase.I added continuosly 8 records till then no issues.After 8 th reocrd i added 9 th one but it's not get refreshed with 9th.I checked prodcutsList length still it is 8 only.When i used localhost no issues why becoz it is giving response very quickly.

                  When i connected to remote server(my company server) sometimes it's getting slow down.(even if localhost also get slow then the same issue will coming into the picture again)This time response is coming back with old data only.Or it may be some other issue??
                  I am thinking that this is the issue.How to solve this?

                  Please help me on this...
                  thanks in advance
                  shailu
                  • 6. Re: HTTPService
                    Level 7

                    "shailuvs" <webforumsuser@macromedia.com> wrote in message
                    news:ghqbaq$3cr$1@forums.macromedia.com...
                    > Hi,
                    >
                    > I also have the same problem in my application.I am trying sample
                    > aplication using httpserivce to display product details in grid.If user
                    > wants
                    > to add new product info
                    > i am using popup window to add.For me also it's inserting into database
                    > fine.No issues from db side.Only thing I am struggling is ui.
                    > My grid is not refreshing everytime.If I add 100 times it is inserted into
                    > database 100 times.But grid will not refresh 100 times.
                    > Some times I need to refresh manually to see latest changes (newly
                    > inserted
                    > record).
                    > How to resolve this?
                    > anybody please help me......

                    How are you adding the items? If you use the ArrayCollection's addItem, the
                    change events should be dispatched as expected to make the dataGrid refresh.


                    • 7. Re: HTTPService
                      Level 7

                      "shailuvs" <webforumsuser@macromedia.com> wrote in message
                      news:ghqta7$pho$1@forums.macromedia.com...
                      > Hi harsha,
                      > ok that's fine from ui.
                      > Another problem need to solve is..
                      > when ever i add a product from popup...the below lines of code getting
                      > executed in my app......
                      >
                      > add.send(productData);
                      > getProducts.send();(this will call result handler method onResult(event))
                      >
                      > private function onResult(event:ResultEvent):void {
                      > prodcutsList = event.result.ProductInfo.ProductList as
                      > ArrayCollection;
                      > datagrid.dataProvider = prodcutsList;
                      > }
                      >
                      > this is the code from popupwindow........
                      > I used alert boxes to debug my application finally i found what's the
                      > problem.Initially prodcutsList variable length is 0.If i add a new product
                      > prodcutsList variable length will increase.I added continuosly 8 records
                      > till
                      > then no issues.After 8 th reocrd i added 9 th one but it's not get
                      > refreshed
                      > with 9th.I checked prodcutsList length still it is 8 only.When i used
                      > localhost
                      > no issues why becoz it is giving response very quickly.
                      >
                      > When i connected to remote server(my company server)
                      > sometimes
                      > it's getting slow down.(even if localhost also get slow then the same
                      > issue
                      > will coming into the picture again)This time response is coming back with
                      > old
                      > data only.Or it may be some other issue??
                      > I am thinking that this is the issue.How to solve this?

                      Check out Q7
                      http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf and see if
                      that helps you.
                      > Please help me on this...
                      > thanks in advance
                      > shailu
                      >


                      • 8. Re: HTTPService
                        shailuvs Level 1
                        Hi,i am not using arraycollections additem
                        i am inserting into database tables using jsps
                        in mxml
                        add.send(addProductData);
                        <mx:Model id="addProductData">
                        <root>
                        <productname>{pn.text}</productname>
                        </root>
                        </mx:Model>
                        • 9. Re: HTTPService
                          shailuvs Level 1
                          >Check out Q7
                          > http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf >and see if
                          >that helps you.

                          I have seen this also...
                          not enough.....
                          • 10. Re: HTTPService
                            ntsiii Level 3
                            First, populating mx:Model declaratively is very hard to debug.

                            I advise using a result handler.

                            Also, avoid mx:Model. Use XML or one of the collections instead.

                            Traqcy
                            • 11. Re: HTTPService
                              ~lordshark

                              Using another words...

                              Isn't there a way to make HTTPService Class work syncronous?

                              Just something like this...


                              var h:HTTPService = new HTTPService();

                              /* set properties */


                              var result:Object = h.send();

                               

                              This manner let the developer handles the data as the way he wants, independant of Events.

                               

                              Sds.