简体   繁体   English

J2ME序列化

[英]J2ME Serialization

I'm writing an application for mobile phones on Java. 我正在为Java编写手机应用程序。 It's goal is to send and receive Vector objects to and from the server. 它的目标是向服务器发送和接收Vector对象。 But here I've got a problem: there's no ObjectOutputStream supported at J2ME.So I have to convert my Vector to byte array or do something of that kind. 但是在这里我遇到了一个问题:J2ME没有支持ObjectOutputStream 。所以我必须将我的Vector转换为byte数组或者做那种事情。

I've been thinking about converting the Vector to string , transmitting it over the network and rebuilding the original vector back from the string, but it hardly seems to work in appropriate forms. 我一直在考虑将Vector转换为string ,通过网络传输它并从字符串重建原始向量,但它似乎很难以适当的形式工作。

Also, I've looked at some frameworks, like J2ME Polish , but unfortunately I failed to find the jar-files with API in the installation folder. 另外,我看了一些框架,比如J2ME Polish ,但遗憾的是我在安装文件夹中找不到带有API的jar文件。

So, any ideas? 那么,有什么想法吗?

There are two relatively easy ways to serialize and deserialize Java objects to their binary representation to facilitate network communication between a server and a Java ME device: 将Java对象序列化和反序列化为二进制表示有两种相对简单的方法,以便于服务器和Java ME设备之间的网络通信:

  1. Protocol Buffers 协议缓冲区

    Protocol Buffers are Google's creation to quickly implement very efficient exchange of data over a network. 协议缓冲区是Google的创建,可以通过网络快速实现非常有效的数据交换。 You define a "protobuf" file that is a specification of the object you want to exchange, and then the protobuf tools create client and server-side stubs to handle the serialization and de-serialization of the objects over a network connection. 您定义了一个“protobuf”文件,它是您要交换的对象的规范,然后protobuf工具创建客户端和服务器端存根,以通过网络连接处理对象的序列化和反序列化。

    Based on the "Third-Party plugin page" , there are several Java ME projects for handling protocol buffers on Java ME. 基于“第三方插件页面” ,有几个Java ME项目用于处理Java ME上的协议缓冲区。 There are also a number of other language libraries, so this approach should give you plenty of server-side implementation options too. 还有许多其他语言库,因此这种方法也应该为您提供大量的服务器端实现选项。 I personally haven't used this approach on Java ME. 我个人没有在Java ME上使用这种方法。

  2. Netbeans' "Mobile Client to Web Application" tools Netbeans的“移动客户端到Web应用程序”工具

    If your server-side implementation is in Java, you can use Netbeans' "Mobile Client to Web Application" tools to generate server-side and client-side stubs to send Java objects over a binary data stream. 如果服务器端实现是Java,则可以使用Netbeans的“Mobile Client to Web Application”工具生成服务器端和客户端存根,以通过二进制数据流发送Java对象。 The above link is a good tutorial for detailed implementation. 以上链接是详细实施的好教程。

    The general steps are: 一般步骤是:

    a) Define your server-side web service in Java EE, including the object you'd like to pass over the network connection. a)在Java EE中定义服务器端Web服务,包括您希望通过网络连接传递的对象。

    b) Run the "Mobile Client to Web Application" tool to generate client and server-side stubs. b)运行“Mobile Client to Web Application”工具以生成客户端和服务器端存根。

    I've used this approach in several Java ME apps and it works very well. 我在几个Java ME应用程序中使用了这种方法,它运行得很好。 The pros for this approach are that you can see and edit all the source code for the generated stubs, as its right there in your project. 这种方法的优点是您可以查看和编辑生成的存根的所有源代码,就像它在项目中一样。 The possible con is that it requires a Java implementation of the server-side code, and isn't as portable to other platforms as protocol buffers are. 可能的缺点是它需要服务器端代码的Java实现,并且不像协议缓冲区那样可移植到其他平台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM