Apache HTTP 客户端或 URLConnection

发布于 2024-10-13 20:04:56 字数 102 浏览 7 评论 0原文

我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许仙没带伞 2024-10-20 20:04:56

自 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/

这是 Android 版 HttpClient 4.1 的重新打包。的版本
Android SDK中的HttpClient是4.0beta2。已经有好几个
更新 HttpClient 和一些急需的错误修复,例如身份验证缓存
自 4.0beta 以来。

由于 Google 已弃用 HttpClient,转而采用 Java 标准
HttpURLConnection 我创建了一个脚本来转换库存版本
Apache 的 HttpClient 变成了 Android 库。

对库存 HttpClient 的更改

  • 将所有包 org.apache.http 重命名为 ch.boye.httpclientandroidlib
  • 删除了所有依赖于 org.ietf.* 的类(SPNEGO 身份验证)
  • 将 org.apache.commons.codec.binary.Base64 替换为 android.util.Base64
  • 创建了一个新类 HttpClientAndroidLog 来替换 org.apache.commons.logging

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/

This is a repackaging of HttpClient 4.1 for Android. The version of
HttpClient in the Android SDK is 4.0beta2. There have been several
updates to HttpClient and some much-needed bugfixes like auth caching
since the 4.0beta.

Since Google has deprecated HttpClient in favor of Java standard
HttpURLConnection I created a script to convert a stock release of
Apache's HttpClient into an Android library.

Changes to stock HttpClient

  • Renamed all packages org.apache.http to ch.boye.httpclientandroidlib
  • Deleted all classes dependent on org.ietf.* (SPNEGO authentication)
  • Replaced org.apache.commons.codec.binary.Base64 with android.util.Base64
  • Created a new class HttpClientAndroidLog to replace org.apache.commons.logging
森林很绿却致人迷途 2024-10-20 20:04:56

对于大多数事情,我认为 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 a URLConnection. Examples of edge cases here and here

EDIT
A similar question has been asked before: httpclient vs httpurlconnection. I would assume that HttpUrlConnection is somewhat faster as the HttpClient is built on top of the standard Java libraries. However I would find HttpClient code much quicker and easier to write and maintain. According to a comments below, the core elements of HttpClient 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.

坠似风落 2024-10-20 20:04:56

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文