【问题标题】:'org.apache.http.HttpEntity' is deprecated. How to solve this error in android studio?'org.apache.http.HttpEntity' 已被弃用。如何在android studio中解决这个错误?
【发布时间】:2015-05-31 10:58:24
【问题描述】:

我正在使用 android studio API22,但出现以下错误:

'org.apache.http.HttpEntity' is deprecated
'org.apache.http.HttpResponse' is deprecated
'org.apache.http.NameValuePair' is deprecated
'org.apache.http.client.HttpClient' is deprecated
'org.apache.http.client.entity.UrlEncodedFormEntity' is deprecated
'org.apache.http.client.methods.HttpPost' is deprecated
'org.apache.http.impl.client.DefaultHttpClient' is deprecated
'org.apache.http.message.BasicNameValuePair' is deprecated
'org.apache.http.params.BasicHttpParams' is deprecated
'org.apache.http.params.HttpConnectionParams' is deprecated
'org.apache.http.params.HttpParams' is deprecated
'org.apache.http.util.EntityUtils' is deprecated

我该如何解决这个问题?

【问题讨论】:

  • 这不是错误。这是警告。您应该阅读文档。必须提示您应该使用哪个类
  • inspection 'Deprecated API usage' 选项:编辑检查配置文件设置,并将类 'HttpEntity' 注释为 @Deprecated:编辑意图设置。我应该选择哪些选项?我真的不知道它们是什么意思

标签: java android apache-httpclient-4.x


【解决方案1】:

将此添加到您的 gradle 中

useLibrary 'org.apache.http.legacy'

例子

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 2
    versionName "1.0.1"
}
buildTypes {
    release {
        debuggable false
        signingConfig signingConfigs.releaseConfig
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debugSigned {
        debuggable true
        signingConfig signingConfigs.releaseConfig
    }
    debug {
        debuggable true
    }
}
useLibrary 'org.apache.http.legacy'
}

【讨论】:

  • msebera 版本是 1.1mb,对于一个小应用程序来说是相当大的开销。旧版本是 297k。 okhttp + 兼容性 jar 总共 382kb - 并且会鼓励切换到现代 API - 但兼容性层并没有涵盖足够的类。
【解决方案2】:

引用myself:

如果您需要继续使用 HttpClient API,请考虑切换到 OkHttp 及其 HttpClient 兼容层,或者考虑切换到 Apache’s separate Android edition of HttpClient。否则,切换到 HttpURLConnection 或 OkHttp 的原生 API。

或者,根据您使用 HttpClient 的目的,使用更具体的网络库(用于 Web 服务的改造、用于图像的 Picasso 或通用图像加载器等)。

另外请注意,HttpClient 已从 M Developer Preview 的 SDK 中移除,表明它将在下一版 Android 中移除。虽然有a workaround to continue using HttpClient in M,但你确实需要换个方向。

【讨论】:

  • API 中的这些类有简单的替代品吗?
  • @lamriassia:我不知道您认为“简单”是什么以及您认为“在 API 中”是什么。我在我的回答中列出了几个替代品(OkHttp 的 HttpClient API、Apache 的替代品 HttpClient、直接使用 OkHttp、HttpUrlConnection、Retrofit、Picasso 和 Universal Image Loader)。您必须自己决定哪些(如果有的话)是“简单的”,哪些(如果有的话)是“在 API 中的”。
  • @CommonsWare 你最后用了什么。你能分享你写的代码吗?
  • 为什么 "useLibrary 'org.apache.http.legacy'" 对使用旧 API 没有帮助?
  • @androiddeveloper:我在一两年前离开了 HttpClient;我没有尝试在 M 开发者预览版中使用 .legacy 的东西。
【解决方案3】:

库存 Android 上提供的 Apache HTTP 客户端版本是 very very old

Google Android 1.0 发布时带有 Apache HttpClient 的预测试版快照。为了配合第一个 Android 版本,Apache HttpClient 4.0 API 不得不提前冻结,而许多接口和内部结构仍未完全制定。随着 Apache HttpClient 4.0 的成熟,该项目期望 Google 将最新的代码改进整合到他们的代码树中。不幸的是,它没有发生。

如果您不想切换到新的 API,您可以手动将新版本的 Apache HttpClient 库添加到您的项目中,以替换 Android SDK

当面向 SDK 23+ 时,最简单的方法是使用 Marek Sebera 的新 Apache HttpClient package for Android(如 suggested by Apache),它可以作为替代品使用。只需将以下依赖项添加到您的 build.gradle 文件(如果合适,更新版本号):

compile "cz.msebera.android:httpclient:4.4.1.1"

并在您的项目中将import org.apache.http.* 替换为import cz.msebera.android.httpclient.*

请注意,旧库中的许多类已被弃用(例如HttpParamsThreadSafeClientConnManager),因此重写代码可能是更好的解决方案。


编辑:在我们更新到较新的 Http 客户端后,我发现了一些用户在代理服务器后面遇到超时异常的情况。由于代码到处都是过时的警告,我们认为尝试解决这个问题是不值得的。我建议在尝试将其投入生产之前进行彻底的测试。

正如其他答案中提到的,更好的解决方案是硬着头皮切换到原生 Android HttpUrlConnection,或者如果这不能满足您的需求,您可以使用库 OkHttp,它HttpUrlConnection 就是 internally based upon anyway

【讨论】:

  • 我们在这里,2015 年 12 月,在我的应用 build.gradle 中添加 cz.msebera.android.httpclient 4.4.1.1 后,Android Studio 1.5, sdk 23 告诉 HttpParams, HttpConnectionParams, BasicHttpParams, HttpProtocolParamsDefaultHttpClient 已弃用。看来我得从头开始重新编码。
【解决方案4】:

HttpClient 已从 android lollypop 5.1 (API 22) 中删除:-
但我们仍然可以通过在 Android Studio 中使用以下代码来使用 HttpClient:-
转到 app\build.gradle 文件:

defaultConfig {
        applicationId "com.XXXXX.XXX"
        minSdkVersion 16
        targetSdkVersion 23
        -----------
        -----------

        useLibrary 'org.apache.http.legacy'
    }

[或替代]


下载HttpClient jar文件并将其添加到您的项目或使用okHttp

【讨论】:

    【解决方案5】:

    在 Gradle 中使用以下依赖项

    dependencies {
        compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    }
    

    【讨论】:

      【解决方案6】:

      here 下载org.apache.http.legacy jar 文件。

      build.gradle 文件中,输入以下代码

      defaultConfig {
            useLibrary 'org.apache.http.legacy'
      }
      

      将下载的 .jar 文件保存在以下位置

      ./sdk/platforms/android-23/optional/(.jar file)
      

      我在平台下使用android-23,这取决于您使用的API级别。如果可选文件夹不存在,请创建可选文件夹并将 .jar 文件粘贴到其中,然后再同步 gradle 项目。

      【讨论】:

        【解决方案7】:

        将此添加到您的 gradle 中

        useLibrary 'org.apache.http.legacy'
        

        例子

        android {
        compileSdkVersion 23
        buildToolsVersion "23.0.1"
        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 23
            versionCode 2
            versionName "1.0.1"
        }
        buildTypes {
            release {
                debuggable false
                signingConfig signingConfigs.releaseConfig
                minifyEnabled true
                shrinkResources true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            debugSigned {
                debuggable true
                signingConfig signingConfigs.releaseConfig
            }
            debug {
                debuggable true
            }
        }
        useLibrary 'org.apache.http.legacy'
        }
        

        这个库可以让你毫无问题地使用它,至少我到目前为止没有任何问题。

        【讨论】:

        • 这看起来像是@bfacumat 答案的精确副本
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-22
        • 2020-10-29
        • 1970-01-01
        • 2019-04-05
        • 2017-03-12
        相关资源
        最近更新 更多