Dalvik to Java SE communications
I'm planning on developing an app for android that requires a back-end server to sync data with other users of the app. I'm planning on writing this server in standard java running on a unix server.
I once did this directly between two android devices, in that case I just serialized all the data needed to be sent on both ends.
However I suspect that the format that Dalvik serializes to and Java SE's format are not compatible. Is this the case? And if it is, what are my alternatives? One thing that popped into my mind was sending ra开发者_开发技巧w xml over a socket, but if there are better alternatives I'll be glad to hear them.
Thanks.
If you are doing a server then you should rely on something more standard like XML or JSON. I personally favor JSON. You shouldn't expect all your client to be Java friendly. Almost every mobile device support JSON. Look at Jackson library to generate your json. Then you can use Jackson again to deserialize your object.
The beauty of this solution is also stupid simple. You can look at the content by just just putting the request in your browser. Not so easy with binary data.
I have used data serialization successfully between Android devices and servers.
I did have to convert TimeZone
class to String and back because the TimeZone
class in particular is not fully compatible (it tried transferring something in the sun.
package which got ClassNotFoundException
on Android).
Other than that I have been able to transfer objects from java.util
collections and maps and from java.sql
data types and of course the java.lang
types String
, Integer
, etc..
You can try protobuf for serialization. It is said to be more efficient, and you won't be concerned about compatibility.
You can also use some form of XML serialization (JAXB, XStream, XMLEncoder, etc)
The resolution of this question hints that it is compatible.
If your object graph is pretty simple and if you are comfortable with JSON at all, Android has JSON support included and it would be easy to get support in Java SE. I tend to think of JSON as a good alternative for when XML or Java serialization seems to "heavy".
Have a look at this benchmark. Kryo is the one I'm using. It supports creation of the custom binary serialization, which can be done in a way suitable for both Dalvik and JSE.
You may want to look at a related question, which provides additional discussion and links.
Protocol buffers would be a good format over the wire to consider.
I can't speak to the serialization of Dalvik.
精彩评论