J2ME serialization

I am writing a mobile phone application in Java. The goal is to send and receive vector objects to and from the server. But here I have a problem: there are no ObjectOutputStream supported in J2ME. So I need to convert the Vector array to byte or do something like that.

I was thinking of converting Vector to string , passing it over the network and restoring the original vector back from the string, but it hardly works in the appropriate forms.

In addition, I looked at some frameworks, such as J2ME Polish , but, unfortunately, I could not find the jar files with the API in the installation folder.

So any ideas?

+4
source share
1 answer

There are two relatively simple ways to serialize and deserialize Java objects to their binary representation to facilitate network communication between the server and the Java ME device:

  • Protocol Buffers

    Protocol buffers are Google’s creation for the rapid implementation of highly efficient network communications. You define the protobuf file, which is the specification of the object you want to exchange, and then the protobuf tools create client and server stubs to handle serialization and de-serialization of objects over a network connection.

    Based on a third-party plug-in page, there are several Java ME projects for processing protocol buffers in Java ME. There are also a number of other language libraries, so this approach will also give you many server-side implementations. I personally have not used this approach in Java ME.

  • Netbeans Mobile Web Application Client Tools

    If your server-side implementation is in Java, you can use Netbeans' Mobile Client for Web Applications "tools to create server-side and client stubs to send Java objects over the binary data stream. The above link is a good guide for a detailed implementation.

    General steps:

    a) Define the server-side server service in Java EE, including the object that you want to transfer over a network connection.

    b) Run the Mobile Client in Web Application tool to create client and server stubs.

    I have used this approach in several Java ME applications and it works very well. The advantages for this approach are that you can see and edit the entire source code for the generated stubs, as it is in your project. A possible conflict is that it requires the implementation of Java code on the server side and is not as portable to other platforms as protocol buffers.

+3
source

Source: https://habr.com/ru/post/1440383/


All Articles