4

I'm writing an application for mobile phones on Java. It's goal is to send and receive Vector objects to and from the server. 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.

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.

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.

So, any ideas?

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
Angstrem
  • 346
  • 1
  • 4
  • 14
  • 2
    Instead of serializing the data using ObjectOutputStream consider using JSON or XML-messages to transfer the data (text messages are much easier to debug than binary data) If you must use binary data (performance/bandwidth), I would recommend using custom binary format instead of default serialization. http://stackoverflow.com/questions/2981296/json-parser-for-j2me – Sami Korhonen Oct 17 '12 at 19:51
  • Thanks, actually it's the most optimal way to use JSON in order to transmit structured text information. If anyone has the same problem, here's the official json site: http://json.org – Angstrem Oct 17 '12 at 22:03

1 Answers1

3

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:

  1. Protocol Buffers

    Protocol Buffers are Google's creation to quickly implement very efficient exchange of data over a network. 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.

    Based on the "Third-Party plugin page", there are several Java ME projects for handling protocol buffers on 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.

  2. Netbeans' "Mobile Client to Web Application" tools

    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. 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.

    b) Run the "Mobile Client to Web Application" tool to generate client and server-side stubs.

    I've used this approach in several Java ME apps and it works very well. 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.

Sean Barbeau
  • 11,496
  • 8
  • 58
  • 111