Android 和 AppEngine Web 服务:Json...RPC、REST...协议缓冲区?

发布于 2024-11-03 07:47:51 字数 249 浏览 2 评论 0原文

我正在开发一个可以从 Android 应用程序访问的网络服务。在对什么是最好的技术进行了一些研究之后,我对这些选项感到有些困惑和茫然。

显然在 Android 端我希望它尽可能的轻量级。我也更愿意分享通用代码,因为两者都是 java,尽管这不太重要。我最关心的是让它高效,然后是简单而优雅的代码。

我在Android端尝试过gson,效果很好。但后来我读到了协议缓冲区,这似乎更有效,我不确定这是否有显着差异。我也不确定是选择 RPC 还是 REST。

I'm working on a web service that will be accessed from an Android app. After doing some research on what's the best technology, I'm left somewhat confused and dazed by the options.

Obviously on the Android end I want it to be as lightweight as possible. I also would prefer to share the common code since both are java, although that's less important. My primary concern is having it be efficient, and after that, simple and elegant code.

I've tried gson on the Android end, and it works nicely. But then I read about protocol buffers, and that seems even more efficient, I'm not sure if it's a significant difference. Also I'm not sure whether to go for RPC or REST.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

桃扇骨 2024-11-10 07:47:51

在效率方面,Protocol Buffers 可能比任何 JSON 实现都更高效,但不一定像您想象的那么高效。 GSON 并不是特别快,但 Jackson 库几乎可以与大多数二进制序列化器竞争(在大多数情况下,Jackson 比 GSON 快 2-4 倍,在 UTF-8 上快 10-20 倍,因为它有针对 UTF-8 的特殊代码)。

但由于编程模型的原因,我仍然会选择 Protocol Buffers 而不是任何 JSON 库。对于大多数 JSON 库,您必须手动检查消息的结构。使用 Protocol Buffers,您以声明方式指定消息结构,库将为您处理结构验证(尽管仍然有一些事情需要您手动验证)。

其他库,如 Protocol Buffers:Apache Avro、Apache Thrift。

Protostuff 库使用 Protocol Buffers 数据模型(因此您可以免费获得结构验证),但除其他格式外还支持序列化为 JSON 和 YAML。如果您希望您的服务由 Javascript 代码使用,这会很有用,其中 JSON 通常是最容易处理的事情。

On the efficiency front, Protocol Buffers will probably be more efficient than any JSON implementation, thought not necessarily by as much as you think. GSON is not particularly fast, but the Jackson library can almost compete with most binary serializers (Jackson is 2-4x faster than GSON in most situations and 10-20x faster on UTF-8 because it has special code for UTF-8).

But I'd still take Protocol Buffers over any JSON library because of the programming model. With most JSON libraries, your have to check the structure of a message manually. With Protocol Buffers, you specify message structures declaratively and the library will take care of the structural validation for you (though there will still be things that you need to validate manually).

Other libraries like Protocol Buffers: Apache Avro, Apache Thrift.

The Protostuff library uses the Protocol Buffers data model (so you get structural validation for free) but support serializing to JSON and YAML in addition to other formats. This can be useful if you want your service to be consumed by Javascript code, where JSON is often the easiest thing to deal with.

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