Dalvik 到 Java SE 通信
我正在计划开发一个 Android 应用程序,它需要后端服务器来与该应用程序的其他用户同步数据。我计划用在 Unix 服务器上运行的标准 java 编写这个服务器。
我曾经直接在两个 Android 设备之间执行此操作,在这种情况下,我只是序列化了两端需要发送的所有数据。
但是我怀疑 Dalvik 序列化的格式和 Java SE 的格式不兼容。是这样吗?如果是的话,我有什么选择?我突然想到的一件事是通过套接字发送原始 xml,但如果有更好的替代方案,我会很高兴听到它们。
谢谢。
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 raw xml over a socket, but if there are better alternatives I'll be glad to hear them.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您正在做服务器,那么您应该依赖更标准的东西,例如 XML 或 JSON。我个人比较喜欢JSON。您不应该期望所有客户端都对 Java 友好。几乎所有移动设备都支持 JSON。查看 Jackson 库来生成 json。然后你可以再次使用 Jackson 来反序列化你的对象。
这个解决方案的优点也很简单。您只需在浏览器中输入请求即可查看内容。对于二进制数据来说就不那么容易了。
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.
我已经在 Android 设备和服务器之间成功使用了数据序列化。
我确实必须将
TimeZone
类转换为 String 并返回,因为TimeZone
类尤其不完全兼容(它尝试在sun 中传输某些内容。 在 Android 上遇到
ClassNotFoundException
的包)。除此之外,我还能够从
java.util
集合和映射以及java.sql
数据类型,当然还有java.lang 类型
String
、Integer
等。I have used data serialization successfully between Android devices and servers.
I did have to convert
TimeZone
class to String and back because theTimeZone
class in particular is not fully compatible (it tried transferring something in thesun.
package which gotClassNotFoundException
on Android).Other than that I have been able to transfer objects from
java.util
collections and maps and fromjava.sql
data types and of course thejava.lang
typesString
,Integer
, etc..您可以尝试使用 protobuf 进行序列化。据说效率更高,而且你不会担心兼容性问题。
您还可以使用某种形式的 XML 序列化(JAXB、XStream、XMLEncoder 等)
的解析 这个问题暗示它是兼容的。
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.
如果你的对象图非常简单并且你对 JSON 很满意,Android 有 包含 JSON 支持,并且很容易在 Java SE 中获得支持。当 XML 或 Java 序列化看起来“繁重”时,我倾向于认为 JSON 是一个很好的替代方案。
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".
查看此基准测试。 Kryo 是我正在使用的。它支持创建自定义二进制序列化,这可以通过适合 Dalvik 和 JSE 的方式完成。
您可能需要查看相关问题,其中提供了额外的讨论和链接。
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.
协议缓冲区是一种值得考虑的良好网络格式。
我无法谈论 Dalvik 的连载。
Protocol buffers would be a good format over the wire to consider.
I can't speak to the serialization of Dalvik.