运行以下文件时,我没有收到任何错误,但是来自以下trace()语句的Java POJO对象返回的数据是:
[object ComputerInfo]
null
[object ComputerInfo]
null
我不知道如何调试它。我试图调整代码from here。
我的客户端文件如下。我的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">
<fx:Script><![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
private var reqId1:int = 0;
public var dataReadFromDB:ComputerInfo = new ComputerInfo;
private function readFaultHandler( event:FaultEvent ):void
{
Alert.show( event.fault.faultString, "Error reading data" );
}
private function readResultHandler( event:ResultEvent ):void
{
dataReadFromDB.javaVersion = event.result.javaVersion;
dataReadFromDB.javaVendor = event.result.javaVendor;
dataReadFromDB.os = event.result.os;
dataReadFromDB.osVersion = event.result.osVersion;
dataReadFromDB.requestId = event.result.requestId;
trace(dataReadFromDB);
trace(dataReadFromDB.javaVersion);
trace(event.result);
trace(event.result.javaVersion);
}
]]></fx:Script>
<fx:Declarations>
<mx:RemoteObject
id="ro"
destination="myDestination"
showBusyCursor="true">
<mx:method name="readData"
result="readResultHandler(event);"
fault="readFaultHandler(event);"/>
</mx:RemoteObject>
</fx:Declarations>
<mx:Panel width="476" height="281"
layout="absolute" title="BlazeDS Example"
cornerRadius="0" backgroundColor="#ffffff">
<s:Label x="46" y="59" text="Click to Read Data" />
<s:Button x="200" y="59" label="Read Now" click="ro.readData( reqId1++ )"/>
</mx:Panel>
</s:Application>
ComputerInfo.as文件:
package {
[RemoteClass(alias="ComputerInfo")]
public class ComputerInfo {
public var javaVersion:String;
public var javaVendor:String;
public var os:String;
public var osVersion:String;
public var requestId:String;
}
}
我的java文件是:
public class MyClass {
public ComputerInfo readData( String requestId ) {
ComputerInfo computerInfo = new ComputerInfo();
computerInfo.javaVersion = "java.vm.version";
computerInfo.javaVendor = "java.vm.vendor";
computerInfo.os = "osname";
computerInfo.osVersion = "os.version";
computerInfo.requestId = "requestId";
return computerInfo;
}
}
和一个名为ComputerInfo.java的单独文件:
public class ComputerInfo {
String javaVersion;
String javaVendor;
String os;
String osVersion;
String requestId;
}
内部remoting-config.xml:
<destination id="myDestination">
<properties>
<source>myClass</source>
</properties>
</destination>
答案 0 :(得分:1)
您似乎错过了数据传输对象(ComputerInfo.as)中的[RemoteClass]元标记:
package {
[RemoteClass(alias="com.myjavapackage.ComputerInfo")]
public class ComputerInfo { ... }
}
(您必须使用适当的值替换“com.myjavapackage”;代码示例中未提及该包)
如果缺少标记,Flex将无法将数据反序列化为适当的类型,而是获得“ObjectProxy”类型的对象。
如果所有内容都已正确注册,您也可以删除自己对结果值的复制 - event.result
已经是ComputerInfo
类型。
修改强>
此外,您的Java类中的字段变量应为public
,或者应该有public
getter和setter方法。
有关详细信息,请参阅Adobe Doc。相关部分是“显式映射ActionScript和Java对象”。
答案 1 :(得分:1)