Apache HTTP 客户端或 URLConnection
我需要在 Android 应用程序上下载网页,但我很难决定是使用 Android Apache HTTP 客户端还是 Java 的 URLConnection。
有什么想法吗?
I need to download a web page on an Android app and I am having a hard time deciding whether to use the Android Apache HTTP client or Java's URLConnection.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自 Gingerbread 以来,Google 已悄悄弃用 Apache HTTP 客户端: http:// android-developers.blogspot.com/2011/09/androids-http-clients.html。虽然他们没有用已弃用的注释来标记它,但他们建议您将 HttpURLConnection 用于新应用程序,因为:
这是我们 [Google] 未来将花费精力的地方
。就我个人而言,我不喜欢这个决定,宁愿坚持使用 HttpClient 4.1+,因为它更快、错误更少并且定期更新。虽然您无法将系统库升级到版本 4.1,但您可以将 HttpClient jar 包含到您的 Android 项目中(作为额外的好处,这将使您不再依赖 Google 错误修复和供应商更新)。然而,有一个陷阱:为了防止与内置库可能发生冲突,您应该使用 JarJar 工具重命名 httpclient 包。原来有人已经这样做了(重新打包的 jar 和 Android 库项目可供下载):
http://code .google.com/p/httpclientandroidlib/
Google has silently deprecated Apache HTTP client usage since Gingerbread: http://android-developers.blogspot.com/2011/09/androids-http-clients.html. And while they didn't mark it with deprecated annotation, they suggest you to use HttpURLConnection for new applications as:
it is where we [Google] will be spending our energy going forward
.Personally I don't like that decision and would rather stick to HttpClient 4.1+, as it is faster, have fewer bugs and is updated regularly. And while you can not upgrade system library to version 4.1, you can include HttpClient jar to your Android project (as the additional benefit this would allow you to not depend on Google bug fixes and vendor updates). There is one pitfall however: to prevent possible collisions with built-in library you should rename httpclient packages using JarJar tool. Turned out someone already did this (repackaged jar and Android library projects are available for download):
http://code.google.com/p/httpclientandroidlib/
对于大多数事情,我认为
HttpClient
是最佳选择。然而,在某些情况和边缘情况下,我会退回到URLConnection
。边缘情况示例此处和此处<强>编辑
之前曾提出过类似的问题:httpclient vs httpurlconnection。
我认为我会发现HttpUrlConnection
会更快一些,因为HttpClient
是构建在标准 Java 库之上的。然而HttpClient
代码编写和维护起来更快、更容易。根据下面的评论,HttpClient
的核心元素已经过性能优化。如果性能是一个主要问题,那么最好的选择是编写两个客户端,一个使用每种方法,然后对它们进行基准测试。如果您这样做,请告诉我们结果。
For most things I'd say that
HttpClient
is the way to go. However there are some situations and edge cases where I'd fall back to aURLConnection
. Examples of edge cases here and hereEDIT
A similar question has been asked before: httpclient vs httpurlconnection.
I would assume thatI would findHttpUrlConnection
is somewhat faster as theHttpClient
is built on top of the standard Java libraries. HoweverHttpClient
code much quicker and easier to write and maintain. According to a comments below, the core elements ofHttpClient
have been performance optimised.If performance is a major concern your best bet is to write two clients, one using each method, then benchmark them both. If you do this, please let us know the results.
在 Gingerbread 及更高版本中,HttpURLConnection 是最佳选择。考虑已弃用 Apache HttpClient。 (另请注意,Android 不使用 HttpClient 4.1,这在另一条评论中提到。)
如果您遇到 Apache HttpClient 速度更快的情况,请在此处将其报告为错误:http://code.google.com/p/android/issues/list
in Gingerbread and later, HttpURLConnection is the way to go. consider Apache HttpClient deprecated. (also note that Android doesn't use HttpClient 4.1, mentioned in another comment.)
if you have a case where Apache HttpClient is faster, report it as a bug here: http://code.google.com/p/android/issues/list