一个学习Android的iOS开发人员提出的两部分问题,他正在开发一个Android项目,该项目将提出从JSON到图像到音频和视频的流媒体下载的各种请求:

在iOS上,我广泛使用AFNetworking项目。Android上有类似的库吗? 我已经阅读了Square的OkHTTP和Retrofit,以及Volley,但还没有使用它们开发的经验。我希望有人能够为每种方法提供一些最佳用例的具体示例。从我所读到的,OkHTTP似乎是三个中最健壮的,并且可以处理这个项目的需求(上面提到过)。


当前回答

添加到已接受的答案和LOG_TAG所说的....对于Volley在后台线程中解析您的数据,您必须子类Request<YourClassName>,因为onResponse方法在主线程上被调用,如果您的响应很大,在主线程上解析可能会导致UI延迟。 在这里阅读如何做到这一点。

其他回答

RoboSpice Vs. Volley

从https://groups.google.com/forum/ # !主题/ robospice / QwVCfY_glOQ

RoboSpice(RS) is service based and more respectful of Android philosophy than Volley. Volley is thread based and this is not the way background processing should take place on Android. Ultimately, you can dig down both libs and find that they are quite similar, but our way to do background processing is more Android oriented, it allow us, for instance, to tell users that RS is actually doing something in background, which would be hard for volley (actually it doesn't at all). RoboSpice and volley both offer nice features like prioritization, retry policies, request cancellation. But RS offers more : a more advanced caching and that's a big one, with cache management, request aggregation, more features like repluging to a pending request, dealing with cache expiry without relying on server headers, etc. RoboSpice does more outside of UI Thread : volley will deserialize your POJOs on the main thread, which is horrible to my mind. With RS your app will be more responsive. In terms of speed, we definitely need metrics. RS has gotten super fast now, but still we don't have figure to put here. Volley should theoretically be a bit faster, but RS is now massively parallel... who knows ? RoboSpice offers a large compatibility range with extensions. You can use it with okhttp, retrofit, ormlite (beta), jackson, jackson2, gson, xml serializer, google http client, spring android... Quite a lot. Volley can be used with ok http and uses gson. that's it. Volley offers more UI sugar that RS. Volley provides NetworkImageView, RS does provide a spicelist adapter. In terms of feature it's not so far, but I believe Volley is more advanced on this topic. More than 200 bugs have been solved in RoboSpice since its initial release. It's pretty robust and used heavily in production. Volley is less mature but its user base should be growing fast (Google effect). RoboSpice is available on maven central. Volley is hard to find ;)

AFNetworking for Android:

快速Android网络在这里

快速Android网络库支持所有类型的HTTP/HTTPS请求,如GET, POST, DELETE, HEAD, PUT, PATCH

快速Android网络库支持下载任何类型的文件

快速Android网络库支持上传任何类型的文件(支持多部分上传)

快速Android网络库支持取消请求

快速Android网络库支持设置优先级为任何请求(LOW, MEDIUM, HIGH, IMMEDIATE)

快速Android网络库支持RxJava

由于它使用OkHttp作为网络层,它支持:

快速Android网络库支持HTTP/2支持允许对同一主机的所有请求共享一个套接字

快速Android网络库使用连接池,减少请求延迟(如果HTTP/2不可用)

透明的GZIP压缩下载大小

快速Android网络库支持响应缓存,完全避免网络重复请求

谢谢:图书馆是我创建的

异步HTTP客户端loopj vs. Volley

我的项目的细节是小的HTTP REST请求,每1-5分钟一次。

我使用异步HTTP客户端(1.4.1)很长一段时间。性能优于使用普通的Apache httpClient或HTTP URL连接。无论如何,新版本的库不适合我:库内部异常切断回调链。

阅读所有的答案激励我尝试一些新的东西。我选择了Volley HTTP库。

使用一段时间后,即使没有测试,我清楚地看到,响应时间下降到1.5倍,2倍的排射。

也许Retrofit比异步HTTP客户端更好?我得试试。 但我确定Volley不适合我。

还有另一个选择: https://github.com/apptik/jus

它像Volley一样是模块化的,但更多的扩展和文档正在改进,支持不同的HTTP堆栈和开箱即用的转换器 它有一个模块来生成服务器API接口映射,比如Retrofit 它还支持JavaRx

以及许多其他方便的功能,如标记,变形金刚等。

我最近发现了一个名为ion的库,它为桌面带来了一些额外的功能。

ion拥有内置的图像下载支持,集成了ImageView, JSON(在GSON的帮助下),文件和非常方便的UI线程支持。

我在一个新项目上使用了它,到目前为止效果还不错。它的使用比Volley或Retrofit简单得多。