如何在一个一个之后做多个异步webservice调用请求

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

我正在使用 Flex 4.6 和 Actionscript 3。这里我想一一调用 3 个服务。

<?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>

我在调用变量 service1 的第一个服务之后的这段代码在 service1 结果事件中设置为 true。但是没有调用 service2。 service2 和 service3 发生了同样的事情。

原因是您将所有内容都写在一个函数中,它只会被调用一次,所以不会发生,试试这个代码

<?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>