Dalvik 到 Java SE 通信

发布于 2024-10-14 04:17:13 字数 272 浏览 7 评论 0原文

我正在计划开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如梦初醒的夏天 2024-10-21 04:17:13

如果您正在做服务器,那么您应该依赖更标准的东西,例如 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.

苦笑流年记忆 2024-10-21 04:17:13

我已经在 Android 设备和服务器之间成功使用了数据序列化。

我确实必须将 TimeZone 类转换为 String 并返回,因为 TimeZone 类尤其不完全兼容(它尝试在 sun 中传输某些内容。 在 Android 上遇到 ClassNotFoundException 的包)。

除此之外,我还能够从 java.util 集合和映射以及 java.sql 数据类型,当然还有 java.lang 类型 StringInteger 等。

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

迷鸟归林 2024-10-21 04:17:13

您可以尝试使用 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.

许久 2024-10-21 04:17:13

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

め七分饶幸 2024-10-21 04:17:13

查看基准测试。 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.

心凉 2024-10-21 04:17:13

协议缓冲区是一种值得考虑的良好网络格式。

我无法谈论 Dalvik 的连载。

Protocol buffers would be a good format over the wire to consider.

I can't speak to the serialization of Dalvik.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文