2012-03-23 112 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