HttpEntity 现在在 Android 上已被弃用,有什么替代方案? [英] HttpEntity is deprecated on Android now, what's the alternative?

查看:40
本文介绍了HttpEntity 现在在 Android 上已被弃用,有什么替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着 Android 5.1 的发布,似乎所有 Apache http 内容都已弃用.看文档没用;他们都说

With the release of Android 5.1, it looks like all the Apache http stuff has been deprecated. Looking at the documentation is useless; they all say

该类在 API 级别 22 中已弃用.请改用 openConnection().请访问此网页了解更多详情.

第一次阅读时这很好,但是当每个被弃用的课程都这么说时,它就没有那么有用了.

Which is fine the first time you read it, but when EVERY class that was deprecated says that, it's not that helpful.

无论如何,对于 HttpEntity 之类的类,特别是 StringEntityMultipartEntity 之类的替代品是什么? 我替换了BasicNameValuePair 用于我自己的 Android Pair<T, S> 类实现,看起来 URLEncoder.encodeURLEncodedUtils,但我不确定如何处理 HttpEntity.

Anyway, what are the alternatives for classes like HttpEntity, specifically StringEntity, and MultipartEntity? I substituted BasicNameValuePair for my own implementation of Android's Pair<T, S> class, and it looks like URLEncoder.encode is a good substitute for URLEncodedUtils, but I'm not sure what to do about HttpEntity.

编辑

我决定重新编写网络内容.将尝试使用 Retrofit 和 OkHttp

I've decided just to re-write the networking stuff. Gonna try to use Retrofit and OkHttp

编辑

认真考虑将您的电话和内容切换到 Retrofit.很漂亮.我很高兴我做到了.有一些障碍,但很酷.

Seriously take a look at switching your calls and stuff to Retrofit. Pretty nifty. I'm glad I did. There were a few hurdles, but it's cool.

推荐答案

您始终可以导入最后一个 Apache Http 客户端并使用它.此外,您可能想看看像 Volley改造,以防万一你可以使用它.如果开始一个新项目,建议使用网络库,因为不需要重新发明轮子.但是,如果您坚持使用 HttpClient,请继续阅读.

You can always import the last Apache Http client and use that. Also, you might want to take a look at a networking library like Volley or Retrofit, just in case you can use that instead. If starting a new project, using a networking library is recommended because there is no need to reinvent the wheel. But if you are stuck with using HttpClient, then read on.

关于 Apache HttpClient 的最新消息(截至 11/07/2015)

Google Android 1.0 发布了 Apache 的预测试版快照客户端.与第一个 Android 发布 Apache 相吻合HttpClient 4.0 API 不得不提前冻结,而许多接口和内部结构仍未完全确定.作为Apache HttpClient 4.0 正在成熟 该项目期望 Google将最新的代码改进合并到他们的代码树中.不幸的是它没有发生.Apache HttpClient 发布的版本与 Android 已经有效地分叉了.最终谷歌决定停止进一步开发他们的叉子,同时拒绝以兼容性为由升级到 Apache HttpClient 的股票版本担心作为做出此类决定的原因.结果那些安卓希望继续使用 Apache HttpClient API 的开发人员Android 无法利用更新的功能和性能改进和错误修复.Android 的 Apache HttpClient 4.3 端口是旨在通过提供官方发布来纠正这种情况与谷歌安卓兼容.鉴于 Android API 23Google 对 HttpClient 的 fork 已被移除此项目已被移除已停产.

Google Android 1.0 was released with a pre-BETA snapshot of Apache HttpClient. To coincide with the first Android release Apache HttpClient 4.0 APIs had to be frozen prematurely, while many of interfaces and internal structures were still not fully worked out. As Apache HttpClient 4.0 was maturing the project was expecting Google to incorporate the latest code improvements into their code tree. Unfortunately it did not happen. Version of Apache HttpClient shipped with Android has effectively become a fork. Eventually Google decided to discontinue further development of their fork while refusing to upgrade to the stock version of Apache HttpClient citing compatibility concerns as a reason for such decision. As a result those Android developers who would like to continue using Apache HttpClient APIs on Android cannot take advantage of newer features, performance improvements and bug fixes. Apache HttpClient 4.3 port for Android was intended to remedy the situation by providing official releases compatible with Google Android. Given that as of Android API 23 Google's fork of HttpClient has been removed this project has been discontinued.

但是,有一个官方的 Apache HttpClient v4.3 的 Android 端口

Android API 22 及更早版本应使用 Apache HttpClient v4.3

Android API 22 and older should use the Apache HttpClient v4.3

dependencies {
         compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' 
}

Android API 23 及更新版本应使用适用于 Android 的 Apache HttpClient 包由维护马雷克·塞贝拉

Android API 23 and newer should use Apache HttpClient packages for Android maintained by Marek Sebera

dependencies {
     compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1' 
}

信息取自 Apache.org

这篇关于HttpEntity 现在在 Android 上已被弃用,有什么替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆