Adobe Flex:如何定期轮询HTTP服务?

时间:2012-07-19 23:21:03

标签: actionscript-3 flex flex4

我有一个嵌入式系统,通过CGI流程提供JSON结果。我正在尝试设置Flex应用。每秒定期轮询一次服务器并更新GUI。我正在尝试下面的方法使用计时器,但我只得到初始结果,后续更新没有。可能出现什么问题?

<?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"
               creationComplete="application1_creationCompleteHandler(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <mx:HTTPService id="getPacketCounts" showBusyCursor="true" resultFormat="text" 
                        url="http://10.1.10.28/cgi-bin/getpacketcounts" 
                        requestTimeout="500"
                        result="getPacketCounts_resultHandler(event)"
                        fault="faultHandler(event)">
        </mx:HTTPService>       
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            private var timer:Timer = new Timer(1000, 0);
            private var counter:int;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                timer.addEventListener(TimerEvent.TIMER, timerListener);
                timer.start();
            }

            private function timerListener(evt:Event):void {
                label.text = "get http://10.1.10.28/cgi-bin/getpacketcounts";
                getPacketCounts.send();
            }

            protected function getPacketCounts_resultHandler(event:ResultEvent):void
            {
                label.text = event.result.toString().substr(0, 60);
            }           

            protected function faultHandler(event:FaultEvent):void
            {
                Alert.show("The server returned error code " + event.statusCode + ".", event.fault.faultString);
            }               
        ]]>
    </fx:Script>
    <s:Label id="label" text="v1.0"/>
</s:Application>

1 个答案:

答案 0 :(得分:1)

虽然我怀疑问题在于Timer与您的HTTPService不同步,但我看不出您的代码有任何明显错误。您的Timer每秒都会触发,但HTTPService可能需要更长的时间才会返回结果,而Timer的下一个标记会覆盖前一个getPacketCounts.send()请求。我对此并不是百分之百确定。

如果我正在编写此应用,我会设置它以便Timer触发一次,重置Timer,发送HTTPService请求,然后在重新启动之前等待响应Timer

   // Make the Timer run once.
   private var timer:Timer = new Timer(1000, 1);

   // ....

   // Add listeners to the Timer and start it
   protected function application1_creationCompleteHAndler(event:FlexEvent):void
   {
       timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerListener);
       timer.start();
   }

   // Send the request
   private function timerListener(e:TimerEvent):void
   {
       timer.reset();
       // Your other stuff
   }

   protected function getPacketCounts_resultHandler(event:ResultEvent):void
   {
       timer.start()
       // Your code
   }           

   protected function faultHandler(event:FaultEvent):void
   {
       timer.start()
       // Your code
   }