如何逐个执行多个异步Web服务调用请求

时间:2016-01-17 11:12:32

标签: actionscript-3 flash flex flex4

我正在使用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也发生了同样的事情。

1 个答案:

答案 0 :(得分:0)

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

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