2012-03-23 110 views
0

我在做一個使用Java + Flex的項目。我創建了Java類並使用Flex遠程對象來調用該方法。當我將所有代碼寫入mxml時,它運行良好。但是當我把這個腳本包裝成一個文件時,有一些好奇的東西。我需要點擊兩次Flex按鈕才能得到遠程對象返回的結果。我認爲我的檔案有問題。需要點擊兩次Flex按鈕才能獲得RemoteObject結果

下面是我的MXML:

<?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="500" minHeight="600"> 
<fx:Declarations> 
    <mx:RemoteObject id="Control" destination="Control" showBusyCursor="true" /> 
</fx:Declarations> 

<fx:Script> 
    <![CDATA[ 
     import com.wntime.ControlUtil; 
     import mx.rpc.events.ResultEvent; 

     private var resultOfCmd:String; 
     private var cmdStr:String; 
     private var ct:ControlUtil = new ControlUtil(); 

     /* invoke as method */ 
     private function test():void 
     { 
      cmdStr = cmdTxt.text; 
      resultOfCmd = ct.exec(cmdStr); 
      cmdConsole.text = resultOfCmd; 
     } 

     /* */ 
     private function exec():void{ 
      cmdStr = cmdTxt.text; 
      Control.execCmd(cmdStr); 
      Control.addEventListener(ResultEvent.RESULT,execCmd_clickHandler); 
     } 

     private function execCmd_clickHandler(event:ResultEvent):void 
     { 
      cmdConsole.text = event.result.toString(); 

     } 

     private function clearCmdConsole():void 
     { 
      cmdConsole.text = ""; 
     } 

    ]]> 
</fx:Script> 

<s:Panel id="CmdPanel" x="70" y="50" width="501" height="350" title="Command Execute Panel"> 
    <s:RichText x="20" y="33" fontSize="14" text="Cmd:"/> 
    <s:TextInput id="cmdTxt" x="60" y="30" width="239"/> 
    <s:Button id="execCmd" x="312" y="30" width="68" label="execute" click="exec()"/> 
    <s:Button x="400" y="30" label="CmdTest" click="test()"/> 
    <s:TextArea id="cmdConsole" x="20" y="85" width="450" editable="false"/> 
    <s:Button x="400" y="250" label="clear" click="clearCmdConsole()"/> 
</s:Panel> 
</s:Application> 

這裏是作爲文件,該文件名爲ControlUtil:

package com.wntime{ 
import mx.rpc.events.FaultEvent; 
import mx.rpc.events.ResultEvent; 
import mx.rpc.remoting.RemoteObject; 

public class ControlUtil 
{ 
    private var cmd:String = null; 
    private var result:String = null; 
    private var roControl:RemoteObject = new RemoteObject(); 

    public function ControlUtil() 
    { 
     roControl.destination = "Control"; 
    } 

    public function exec(_cmd:String):String{ 
     this.cmd = _cmd; 
     roControl.execCmd(cmd); 
     roControl.addEventListener(FaultEvent.FAULT, execCmd); 
     roControl.addEventListener(ResultEvent.RESULT, execCmd); 
     return result; 
    } 

    public function execCmd(event:ResultEvent):void 
    { 
     setResult(event.result.toString()); 
    } 

    public function setResult(_result:String):void 
    { 
     this.result = _result; 
    } 

    } 
} 

如果我點擊執行按鈕。結果將直接顯示在控制檯(textarea)中。 但我需要點擊兩次CmdTest按鈕才能在控制檯中顯示結果。

給我一個手plz.Thanks提前。

+0

等待第一次點擊後的反應。 – 2012-03-23 08:31:41

回答

1

這只是一個瘋狂的猜測,但我認爲您在Java端調用的方法返回的速度比添加偵聽器的速度快,因此不會調用事件處理程序。第二次所有聽衆都已到位並且您的通話成功。嘗試在調用遠程方法之前添加監聽器。

+0

我覺得有點合理.thx – 2012-03-26 01:45:26

0

你的代碼中有各種錯誤/問題在我看來,我會做這樣的事情:

package com.wntime{ 
import mx.rpc.events.FaultEvent; 
import mx.rpc.events.ResultEvent; 
import mx.rpc.remoting.RemoteObject; 

public class ControlUtil 
{ 
    private var cmd:String = null; 
    private var result:String = null; 
    private var roControl:RemoteObject = new RemoteObject(); 
    // the callBack function is the function that is called when the 
    // remoteobject successfully or not complete the request... 
    // you can set as parameters anything you want... 
    private var callBack:Function = null; 
    public function ControlUtil() 
    { 
     roControl.destination = "Control"; 
    } 

    public function exec(callBack:Function, _cmd:String):void{ 
     this.cmd = _cmd; 
     this.callBack = callBack; 
     roControl.addEventListener(FaultEvent.FAULT, errorCmd); 
     roControl.addEventListener(ResultEvent.RESULT, execCmd); 
     roControl.execCmd(cmd); 
    } 

    private function execCmd(event:ResultEvent):void 
    {   
     callBack(true,event.result.toString()); 
    } 

    private function errorCmd(event:FaultEvent):void 
    { 
     callBack(false, event.error); // call the callBack function passing the value you need 
    } 

} 
} 

回調函數是類似的東西:

private function name(b:Boolean, s:String = null){....} 

*編輯*

從您調用exec命令的主代碼...

// function invoked when the button is clicked! 
private function buttonClick():void 
{ 
    var tmp:ControlUtil = new ControlUtil(); 
    //exec(callBack:Function, _cmd:String) 
    //you pass the function as a reference so when the async request is terminated the function is invoked and you can parse the result.... 
    tmp.exec(getResult, "cmqString"); 
} 

// callBack function for the method ControlUtil.exec 
private function getResult(b:Boolean, result:String = ""):void 
{ 
    if (b) 
    { 
     // the call returned correctly and the result variable contains the value. 
    } 
    else 
    { 
     // the call failed and the result variable contains the error 
    } 
} 

,因爲我指定它在ControlUtil當我用callBack(true/false, result/error)

爲您喜歡,您可以創建功能無論是布爾值和結果值返回...

+0

我是新手。那麼回調函數實際上應該是什麼?回調函數的功能是什麼?在我看來,也許它可能只是一個傳遞一個值的setter函數。是對的嗎? – 2012-03-26 03:26:18

+0

我編輯了我的回覆...希望這可以幫助... – Marcx 2012-03-26 07:18:28