Search code examples
actionscript-3flashapache-flexflex4

How to do multiple async webservice call requests after one by one


I am using Flex 4.6 and Actionscript 3. Here i want to call 3 service one by one.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.rpc.AsyncToken;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;

        public var service1:Boolean = false;
        public var service2:Boolean = false;
        public var service3:Boolean = false;


        protected function btnCall_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            var async1:AsyncToken = http1.send();

            if (service1==true){
                var async2:AsyncToken = http2.send();
            }

            if (service2==true){
                var async3:AsyncToken = http3.send();
            }

            if (service3==true){
                Alert.show("All the service is executed.");
            }

        }

        protected function httpservice1_resultHandler(event:ResultEvent):void
        {
            // TODO Auto-generated method stub
            service1=true;
            Alert.show("Service1:"+ event.result.toString());
        }


        protected function httpservice2_resultHandler(event:ResultEvent):void
        {
            // TODO Auto-generated method stub
            service2=true;
            Alert.show("Service2:"+ event.result.toString());
        }

        protected function httpservice3_resultHandler(event:ResultEvent):void
        {
            // TODO Auto-generated method stub
            service3=true;
            Alert.show("Service3:"+ event.result.toString());
        }

        protected function httpservice1_faultHandler(event:FaultEvent):void
        {
            // TODO Auto-generated method stub
            Alert.show(event.fault.message);
        }


    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <s:HTTPService id="http1" url="http://localhost/checkfile.php" method="POST" result="httpservice1_resultHandler(event)" fault="httpservice1_faultHandler(event)"/>
    <s:HTTPService id="http2" url="http://localhost/checkfile.php" method="POST" result="httpservice2_resultHandler(event)" fault="httpservice1_faultHandler(event)"/>
    <s:HTTPService id="http3" url="http://localhost/checkfile.php" method="POST" result="httpservice3_resultHandler(event)" fault="httpservice1_faultHandler(event)"/>
</fx:Declarations>

<s:Button id="btnCall" label="Call Service" click="btnCall_clickHandler(event)"/>
</s:Application>

I this code after 1st service called the variable service1 is set to true in service1 result event. but service2 is not called. Same thing happened for service2 and service3.


Solution

  • The reason is that you wrote everything in a single function it will be called only once , so it wont happen , try this code

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.AsyncToken;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
    
            public var service1:Boolean = false;
            public var service2:Boolean = false;
            public var service3:Boolean = false;
    
    
            protected function btnCall_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                var async1:AsyncToken = http1.send();
            }
    
            protected function httpservice1_resultHandler(event:ResultEvent):void
            {
                // TODO Auto-generated method stub
                service1=true;
                var async2:AsyncToken = http2.send();
                Alert.show("Service1:"+ event.result.toString());
            }
    
    
            protected function httpservice2_resultHandler(event:ResultEvent):void
            {
                // TODO Auto-generated method stub
                service2=true;
                var async3:AsyncToken = http3.send();
                Alert.show("Service2:"+ event.result.toString());
            }
    
            protected function httpservice3_resultHandler(event:ResultEvent):void
            {
                // TODO Auto-generated method stub
                service3=true;
                if (service3==true){
                    Alert.show("All the service is executed.");
                }
                Alert.show("Service3:"+ event.result.toString());
            }
    
            protected function httpservice1_faultHandler(event:FaultEvent):void
            {
                // TODO Auto-generated method stub
                Alert.show(event.fault.message);
            }
    
    
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <s:HTTPService id="http1" url="http://localhost/checkfile.php" method="POST" result="httpservice1_resultHandler(event)" fault="httpservice1_faultHandler(event)"/>
        <s:HTTPService id="http2" url="http://localhost/checkfile.php" method="POST" result="httpservice2_resultHandler(event)" fault="httpservice1_faultHandler(event)"/>
        <s:HTTPService id="http3" url="http://localhost/checkfile.php" method="POST" result="httpservice3_resultHandler(event)" fault="httpservice1_faultHandler(event)"/>
    </fx:Declarations>
    
    <s:Button id="btnCall" label="Call Service" click="btnCall_clickHandler(event)"/>
    </s:Application>