Android 和 AppEngine Web 服务:Json...RPC、REST...协议缓冲区?
我正在开发一个可以从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在效率方面,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.