0

我正在使用 Java + Flex 做一个项目。我创建了 Java 类并使用 Flex 远程对象来调用该方法。当我在 mxml 中编写所有代码时,它运行良好。但是当我将脚本包装在 as 文件中时,有些奇怪。我需要在 Flex 按钮上单击两次才能获得远程对象返回的结果。我认为我的 as 文件有问题。

下面是我的 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 的 as 文件:

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;
    }

    }
}

如果我单击执行按钮。结果将直接显示在控制台(文本区域)中。但我需要在 CmdTest 按钮上单击两次才能将结果显示在控制台中。

请帮帮我。在此先感谢。

4

2 回答 2

1

这只是一个疯狂的猜测,但我认为您在 Java 端调用的方法返回的速度比您添加侦听器的速度要快,因此不会调用事件处理程序。第二次所有侦听器都到位并且您的呼叫成功。在调用远程方法之前尝试添加侦听器。

于 2012-03-23T09:15:41.203 回答
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)

您可以根据需要创建函数...

于 2012-03-23T19:57:59.637 回答