一个学习Android的iOS开发人员提出的两部分问题,他正在开发一个Android项目,该项目将提出从JSON到图像到音频和视频的流媒体下载的各种请求:
在iOS上,我广泛使用AFNetworking项目。Android上有类似的库吗? 我已经阅读了Square的OkHTTP和Retrofit,以及Volley,但还没有使用它们开发的经验。我希望有人能够为每种方法提供一些最佳用例的具体示例。从我所读到的,OkHTTP似乎是三个中最健壮的,并且可以处理这个项目的需求(上面提到过)。
一个学习Android的iOS开发人员提出的两部分问题,他正在开发一个Android项目,该项目将提出从JSON到图像到音频和视频的流媒体下载的各种请求:
在iOS上,我广泛使用AFNetworking项目。Android上有类似的库吗? 我已经阅读了Square的OkHTTP和Retrofit,以及Volley,但还没有使用它们开发的经验。我希望有人能够为每种方法提供一些最佳用例的具体示例。从我所读到的,OkHTTP似乎是三个中最健壮的,并且可以处理这个项目的需求(上面提到过)。
当前回答
我最近发现了一个名为ion的库,它为桌面带来了一些额外的功能。
ion拥有内置的图像下载支持,集成了ImageView, JSON(在GSON的帮助下),文件和非常方便的UI线程支持。
我在一个新项目上使用了它,到目前为止效果还不错。它的使用比Volley或Retrofit简单得多。
其他回答
从我与Volley合作的经验来看,我想补充一点:
Volley does not handle streaming uploads or downloads in any sense. That is, the entire request body has to be in memory and you cannot use an OutputStream to write the request body to the underlying socket, nor can you use an InputStream to read the response body, as basic HttpURLConnection does. So, Volley is a poor choice for uploading or downloading large files. Your requests and responses should be small. This is one of the biggest limitations of Volley that I have personally encountered. For what it's worth, OkHttp does have interfaces for working with streams. The lack of official documentation is annoying, although I have been able to work around that by reading the source code, which is pretty easy to follow. What is more bothersome is that, as far as I can tell, Volley has no official release versions and no Maven or Gradle artifact, and therefore managing it as a dependency becomes more of a headache than, say, any of the libraries Square has released. You just clone a repo, build a jar, and you're on your own. Looking for a bug fix? Fetch and hope it's there. You might get some other stuff, too; it won't be documented. In my opinion, this effectively means that Volley is an unsupported 3rd party library, even though the code base is reasonably active. Caveat emptor. As a nit, having the Content-Type tied to the class/request type (JsonObjectRequest, ImageRequest, etc.) is kind of awkward and reduces the flexibility of the calling code a bit, as you are tied to Volley's existing Request type hierarchy. I like the straightforwardness of just setting Content-Type as a header like any other (don't do this with Volley, by the way; you'll end up with two Content-Type headers!). That's just my personal opinion, though, and it can be worked around.
这并不是说Volley没有一些有用的功能。的确如此。易于定制的重试策略、透明缓存、取消API以及对请求调度和并发连接的支持都是很棒的特性。但是要知道它并不适用于所有的HTTP用例(参见上面的第1项),而且在应用程序中将Volley投入生产使用(第2项)会有一些令人头疼的问题。
我希望有人能够为每种方法提供一些最佳用例的具体示例。
如果您正在与Web服务通信,请使用Retrofit。如果您正在下载图像,请使用毕加索对等库。如果需要在Retrofit/Picasso之外执行HTTP操作,请使用OkHTTP。
Volley与Retrofit + Picasso大致竞争。好的方面是,它是一个库。缺点是,它是一个没有文档的、不受支持的、“把代码扔到墙上,然后在上面做一个I|O演示”的库。
编辑-排射现在正式支持谷歌。请参考谷歌开发者指南
据我所知,OkHTTP似乎是这三个中最健壮的
如果可以,Retrofit会自动使用OkHTTP。杰克·沃顿有个要点能把Volley和OkHTTP联系起来。
并能处理这个项目的要求(如上所述)。
根据“流媒体”的传统定义,你可能不会使用它们中的任何一个来“流式下载音频和视频”。相反,Android的媒体框架将为你处理这些HTTP请求。
也就是说,如果您打算尝试自己的基于http的流,OkHTTP应该处理这种情况;我不记得Volley会如何处理这种情况。无论是Retrofit还是Picasso都不是为此而设计的。
还有另一个选择: https://github.com/apptik/jus
它像Volley一样是模块化的,但更多的扩展和文档正在改进,支持不同的HTTP堆栈和开箱即用的转换器 它有一个模块来生成服务器API接口映射,比如Retrofit 它还支持JavaRx
以及许多其他方便的功能,如标记,变形金刚等。
添加到已接受的答案和LOG_TAG所说的....对于Volley在后台线程中解析您的数据,您必须子类Request<YourClassName>,因为onResponse方法在主线程上被调用,如果您的响应很大,在主线程上解析可能会导致UI延迟。 在这里阅读如何做到这一点。
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网络库支持响应缓存,完全避免网络重复请求
谢谢:图书馆是我创建的