Actionscript 3 Flex 4.5 序列化/反序列化问题?

发布于 2024-12-03 21:18:32 字数 2309 浏览 0 评论 0原文

我在反序列化 Flex 4.5 Mobile 项目中的对象时遇到一个特殊问题。我已经连接到一个 Web 服务并填充了一个列表框。我可以选择该项目并获取详细信息,但在序列化对象并尝试反序列化之后;对象定义在某个地方丢失了。

我有一个变量,用于当用户在列表中选择请求时,

private var selectedReq:ServiceRequest;
//Here we instantiate the local variable when user select id in ListBox
selectedReq = event.currentTarget.selectedItem as ServiceRequest;           

用户选择保存的每个服务请求都会调用此方法。

            private function writeServiceRequest():void {
            var filename:String = buildFileName();

            var file:File = File.applicationStorageDirectory.resolvePath(filename);
            if (file.exists)
                file.deleteFile();
            var fileStream:FileStream = new FileStream();
            fileStream.open(file, FileMode.WRITE);

//selectedReq is the private var of the users selected item
            fileStream.writeObject(selectedReq); 
            fileStream.close();
        }

当用户想要查看请求时,调用此方法。

        private function readServiceRequest():ServiceRequest {
            var filename:String = buildFileName();
            var file:File = File.applicationStorageDirectory.resolvePath(filename);
            if (!file.exists) {
                return null;
            }

            var fileStream:FileStream = new FileStream();
            fileStream.open(file, FileMode.READ);
            var objReq:ServiceRequest = fileStream.readObject() as ServiceRequest;

            fileStream.close();

            return objReq;
        }

类对象类似于。

public var id:uint;
public var requisitioner:String;
public var requestItems:ArrayCollection //Webservice it's actually List<requestItems>
public var requestProcesses:ArrayCollection // WSDL it's actually List<>

当我尝试读取/反序列化时,例如

                 //This line is null but the file exist and the object was written
            var objReq:ServiceRequest = readServiceRequest() as ServiceRequest;

            if(objReq) {
                selectedReq = objReq;
            }

如果我不将 readServiceRequest() 转换为 ServiceRequest 并简单地返回一个对象;我可以迭代该对象并获取从序列化对象返回的正确值。

我只能假设 Flex 从 Web 服务创建的类可能会导致此问题?如果写入的是值而不是对象类型,那么序列化过程中一定会丢失一些东西 - 对吗?

对于所有细节感到抱歉,但我现在有点迷失......任何帮助将不胜感激。

RL

I'm having a particular issue deserializing an object in a Flex 4.5 Mobile project. I've connected to a Webservice fine and populate a ListBox fine. I can select the item and get the details just fine as well but after serializing the object and trying to deserialize it; the Object definition is getting lost somewhere.

I have a variable for when the user selected the request in a List

private var selectedReq:ServiceRequest;
//Here we instantiate the local variable when user select id in ListBox
selectedReq = event.currentTarget.selectedItem as ServiceRequest;           

Each Service Request the user chooses to save will call this method.

            private function writeServiceRequest():void {
            var filename:String = buildFileName();

            var file:File = File.applicationStorageDirectory.resolvePath(filename);
            if (file.exists)
                file.deleteFile();
            var fileStream:FileStream = new FileStream();
            fileStream.open(file, FileMode.WRITE);

//selectedReq is the private var of the users selected item
            fileStream.writeObject(selectedReq); 
            fileStream.close();
        }

When the users want to view the request this method is called.

        private function readServiceRequest():ServiceRequest {
            var filename:String = buildFileName();
            var file:File = File.applicationStorageDirectory.resolvePath(filename);
            if (!file.exists) {
                return null;
            }

            var fileStream:FileStream = new FileStream();
            fileStream.open(file, FileMode.READ);
            var objReq:ServiceRequest = fileStream.readObject() as ServiceRequest;

            fileStream.close();

            return objReq;
        }

The Class Object is similar to.

public var id:uint;
public var requisitioner:String;
public var requestItems:ArrayCollection //Webservice it's actually List<requestItems>
public var requestProcesses:ArrayCollection // WSDL it's actually List<>

When I try to read/deserialize like

                 //This line is null but the file exist and the object was written
            var objReq:ServiceRequest = readServiceRequest() as ServiceRequest;

            if(objReq) {
                selectedReq = objReq;
            }

If I do not cast the readServiceRequest() as ServiceRequest and simply return an Object; I can iterate through the Object and get the correct values returned from the serialized object.

I can only assume the Classes that Flex created from the Webservice may be causing this? If the values are getting written but not the object type then something has to be lost in the serialization - correct?

Sorry for all the details but I'm a little lost at this time.....any help would be appreciated.

RL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水染的天色ゝ 2024-12-10 21:18:32

var objReq:ServiceRequest = readServiceRequest() as ServiceRequest;

上面的行将继续返回 null。

我敢打赌,如果您按以下方式修改它:

var objReq:ServiceRequest = ServiceRequest(readServiceRequest());

您将收到一个运行时异常,其中包含类似于 Can' 的消息t 将 ObjectProxy 转换为 ServiceRequest

如果是这种情况,那么您收到此消息的原因是 AMF 序列化程序在序列化 ServiceRequest 实例时不保留类型信息。

为了解决这个问题,您需要调用 flash.net.registerClassAlias() 在序列化/反序列化之前。

flash.net.registerClassAlias("完全限定名称.ServiceRequest", ServiceRequest);

var objReq:ServiceRequest = readServiceRequest() as ServiceRequest;

The above line will continue to return null.

I bet that if you modify it in the following way:

var objReq:ServiceRequest = ServiceRequest(readServiceRequest());

You'll get a run-time exception with a message similar to Can't cast ObjectProxy to ServiceRequest

If that's the case, then the reason you get this is because the AMF serializer doesn't preserve the type information when serializing the ServiceRequest-instance.

In order to fix this you need to call flash.net.registerClassAlias() before the serialization/deserialization.

flash.net.registerClassAlias("fully.qualified.name.ServiceRequest", ServiceRequest);

掩耳倾听 2024-12-10 21:18:32

试试这个是否有效。

  1. 检查event.result是否为ByteArray

  2. 将 event.result 读取/存储为 ByteArray。
    var byteArray:ByteArray = event.result as ByteArray;

  3. 使用 readObject() 函数获取/反序列化对象。
    var object:Object = byteArray.readObject();

  4. 转换为目标对象类型。在您的情况下的服务请求。
    var tartgettedObject:ServiceRequest = 对象作为 ServiceRequest;
    或者
    var tartgettedObject:ServiceRequest = ServiceRequest(object);

Try if this work.

  1. Check if event.result is ByteArray.

  2. Read/store the event.result as ByteArray.
    var byteArray:ByteArray = event.result as ByteArray;

  3. Get/Deserialize Object using readObject() function.
    var object:Object = byteArray.readObject();

  4. Cast to targetted object type. ServiceRequest in your case.
    var tartgettedObject:ServiceRequest = object as ServiceRequest;
    or
    var tartgettedObject:ServiceRequest = ServiceRequest(object);

于我来说 2024-12-10 21:18:32

我很抱歉在发布问题之前没有登录。

我确实使用了 registerClassAlias ,但这没有帮助。我确实在 Adob​​e 论坛上发现了其他类似的问题,并且不断的反馈是该问题很可能是由于对象类在文件写入期间丢失其属性造成的,原因是类对象是在从 Web 服务创建数据源时写入的。由于我最初的问题,我们决定使用 SQLite,它工作得很好 - 感谢您的帮助。

RL

My bad for not signing in before posting the question.

I did use registerClassAlias which did not help. I did find on the Adobe forum other simular issues and the constant feedback was the problem is most likely caused by the Object Class loosing it's attributes during the file write reason being the Class Objects are written when the Datasource is created from the Webservice. Since my original question we decided to use SQLite which is working perfectly fine - thanks for all you help.

RL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文