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

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


当前回答

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不适合我。

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网络库支持响应缓存,完全避免网络重复请求

谢谢:图书馆是我创建的

Retrofit 1.9.0 vs RoboSpice

我在我的应用程序中使用这两种。

当我解析嵌套的JSON类时,Robospice比Retrofit工作得更快。因为Spice Manger会为你做任何事。在Retrofit中,您需要创建GsonConverter并反序列化它。

我在同一个活动中创建了两个片段,并使用两个相同类型的url同时调用。

09-23 20:12:32.830  16002-16002/com.urbanpro.seeker E/RETROFIT﹕   RestAdapter Init
09-23 20:12:32.833  16002-16002/com.urbanpro.seeker E/RETROFIT﹕ calling the method
09-23 20:12:32.837  16002-16002/com.urbanpro.seeker E/ROBOSPICE﹕ initialzig spice manager
09-23 20:12:32.860  16002-16002/com.urbanpro.seeker E/ROBOSPICE﹕ Executing the method
09-23 20:12:33.537  16002-16002/com.urbanpro.seeker E/ROBOSPICE﹕ on SUcceess
09-23 20:12:33.553  16002-16002/com.urbanpro.seeker E/ROBOSPICE﹕ gettting the all contents
09-23 20:12:33.601  16002-21819/com.urbanpro.seeker E/RETROFIT﹕ deseriazation starts
09-23 20:12:33.603  16002-21819/com.urbanpro.seeker E/RETROFIT﹕ deseriazation ends

看看Volley透视图,这里有一些适合您需求的优点:

一方面,Volley完全专注于处理单个的小HTTP请求。因此,如果您的HTTP请求处理有一些怪癖,Volley可能为您提供了一个钩子。另一方面,如果您在图像处理方面有一个怪癖,那么您拥有的唯一真正的钩子就是ImageCache。“不是没有,但也不多!””。但它有更多的其他优点,比如一旦你定义了你的请求,从一个片段或活动中使用它们是轻松的,不像并行AsyncTasks

Volley的优点和缺点:

那么Volley的优点是什么呢?

The networking part isn’t just for images. Volley is intended to be an integral part of your back end. For a fresh project based off of a simple REST service, this could be a big win. NetworkImageView is more aggressive about request cleanup than Picasso, and more conservative in its GC usage patterns. NetworkImageView relies exclusively on strong memory references, and cleans up all request data as soon as a new request is made for an ImageView, or as soon as that ImageView moves offscreen. Performance. This post won’t evaluate this claim, but they’ve clearly taken some care to be judicious in their memory usage patterns. Volley also makes an effort to batch callbacks to the main thread to reduce context switching. Volley apparently has futures, too. Check out RequestFuture if you’re interested. If you’re dealing with high-resolution compressed images, Volley is the only solution here that works well. Volley can be used with Okhttp (New version of Okhttp supports NIO for better performance ) Volley plays nice with the Activity life cycle.

截击的问题: 由于Volley是新的,一些东西还不支持,但它是固定的。

Multipart Requests (Solution: https://github.com/vinaysshenoy/enhanced-volley) status code 201 is taken as an error, Status code from 200 to 207 are successful responses now.(Fixed: https://github.com/Vinayrraj/CustomVolley) Update: in latest release of Google volley, the 2XX Status codes bug is fixed now!Thanks to Ficus Kirkpatrick! it's less documented but many of the people are supporting volley in github, java like documentation can be found here. On android developer website, you may find guide for Transmitting Network Data Using Volley. And volley source code can be found at Google Git To solve/change Redirect Policy of Volley Framework use Volley with OkHTTP (CommonsWare mentioned above)

你也可以阅读这比较Volley的图像加载与毕加索

改造:

它是由Square发布的,它提供了非常容易使用的REST API(更新:瞧!蔚来汽车支持)

改造的优点:

与Volley相比,Retrofit的REST API代码更简洁,并提供了 优秀的API文档,在社区中有很好的支持! 它很容易添加到项目中。 我们可以将它用于任何序列化库,并进行错误处理。

更新:

There are plenty of very good changes in Retrofit 2.0.0-beta2 version 1.6 of Retrofit with OkHttp 2.0 is now dependent on Okio to support java.io and java.nio which makes it much easier to access, store and process your data using ByteString and Buffer to do some clever things to save CPU and memory. (FYI: This reminds me of the Koush's OIN library with NIO support!) We can use Retrofit together with RxJava to combine and chain REST calls using rxObservables to avoid ugly callback chains (to avoid callback hell!!).

版本1.6的缺点:

内存相关的错误处理功能不太好(在旧版本的Retrofit/OkHttp中),不确定它是否在Java NIO支持的Okio中得到了改进。 如果我们使用这个方法,最小的线程帮助可能导致回调地狱 以不恰当的方式

(以上所有缺点都在新版本的Retrofit 2.0 beta中得到解决)

========================================================================

更新:

Android Async vs Volley vs Retrofit性能基准(毫秒,值越低越好):

Library One Discussion Dashboard (7 requests) 25 Discussions
AsyncTask 941 ms 4,539 ms 13,957 ms
Volley 560 ms 2,202 ms 4,275 ms
Retrofit 312 ms 889 ms 1,059 ms

(供参考,上面的改装基准信息将改善java NIO支持,因为新版本的OKhttp依赖于NIO Okio库)

In all three tests with varying repeats (1 – 25 times), Volley was anywhere from 50% to 75% faster. Retrofit clocked in at an impressive 50% to 90% faster than the AsyncTasks, hitting the same endpoint the same number of times. On the Dashboard test suite, this translated into loading/parsing the data several seconds faster. That is a massive real-world difference. In order to make the tests fair, the times for AsyncTasks/Volley included the JSON parsing as Retrofit does it for you automatically. RetroFit Wins in benchmark test! In the end, we decided to go with Retrofit for our application. Not only is it ridiculously fast, but it meshes quite well with our existing architecture. We were able to make a parent Callback Interface that automatically performs error handling, caching, and pagination with little to no effort for our APIs. In order to merge in Retrofit, we had to rename our variables to make our models GSON compliant, write a few simple interfaces, delete functions from the old API, and modify our fragments to not use AsyncTasks. Now that we have a few fragments completely converted, it’s pretty painless. There were some growing pains and issues that we had to overcome, but overall it went smoothly. In the beginning, we ran into a few technical issues/bugs, but Square has a fantastic Google+ community that was able to help us through it.

什么时候使用凌空?!

当我们需要加载图像以及使用REST api时,我们可以使用Volley !,网络呼叫排队系统需要同时处理多个n/w请求!Volley也有更好的内存相关错误处理比Retrofit!

OkHttp可以使用Volley, Retrofit默认使用OkHttp !它有SPDY支持,连接池,磁盘缓存,透明压缩!最近,它通过Okio库获得了java NIO的一些支持。

来源:Josh Ruesch先生的volley-vs-retrofit

注意:关于流,它取决于你想要什么类型的流,比如RTSP/RTCP。