0

我有一个嵌入式系统,通过 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>
4

1 回答 1

1

尽管我怀疑问题在于您TimerHTTPService. 您Timer每秒都会触发一次,但您可能需要更长的时间HTTPService才能返回结果,并且您的下一个滴答声会Timer践踏上一个getPacketCounts.send()请求。不过,我对此不是 100% 确定的。

如果我正在编写这个应用程序,我会对其进行设置,以便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
   } 
于 2012-07-20T01:03:14.400 回答