Java 'org.apache.http.HttpEntity' 已弃用。如何解决android studio中的这个错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30556605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:48:56  来源:igfitidea点击:

'org.apache.http.HttpEntity' is deprecated. How to solve this error in android studio?

javaandroidapache-httpclient-4.x

提问by Sml

i'm using android studio API22 and i have these errors:

我正在使用 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

How can i solve this?

我该如何解决这个问题?

回答by CommonsWare

Quoting myself:

引用我自己

If you need to continue using the HttpClient API, consider switching to OkHttpand their HttpClient compatibility layer, or consider switching to Apache's separate Android edition of HttpClient. Otherwise, switch to HttpURLConnection or OkHttp's native API.

如果您需要继续使用 HttpClient API,请考虑切换到OkHttp及其 HttpClient 兼容层,或者考虑切换到Apache 单独的 Android 版本的 HttpClient。否则,切换到 HttpURLConnection 或 OkHttp 的原生 API。

Or, depending upon what you are using HttpClient for, use a more specific networking library (Retrofit for Web services, Picasso or Universal Image Loader for images, etc.).

或者,根据您使用 HttpClient 的目的,使用更具体的网络库(用于 Web 服务的 Retrofit、用于图像的 Picasso 或 Universal Image Loader 等)。

Also note that HttpClient is removed from the SDK for the M Developer Preview, indicating that it will be removed in the next edition of Android. While there is a workaround to continue using HttpClient in M, you really need to move to something else.

还要注意 HttpClient 从 M Developer Preview 的 SDK 中删除,表示将在下一版 Android 中删除。虽然有一种解决方法可以继续在 M 中使用 HttpClient,但您确实需要转移到其他地方。

回答by Tim Rae

The version of the Apache HTTP client provided on stock Android was very very old.

现有 Android 上提供的 Apache HTTP 客户端版本非常非常旧

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.

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

If you don't want to switch over to a new API you can manually add a newer version of the Apache HttpClient library into your project to replace the old deprecated version in Android SDK < 22.

如果您不想切换到新 API,您可以手动将较新版本的 Apache HttpClient 库添加到您的项目中,以替换 Android SDK < 22 中已弃用的旧版本。

The easiest way to do this when targeting SDK 23+ is to use Marek Sebera's new Apache HttpClient package for Android(as suggested by Apache), which could potentially work as a drop-in replacement. Simply add the following dependency to your build.gradle file (updating the version number if appropriate):

在针对 SDK 23+ 时执行此操作的最简单方法是使用 Marek Sebera 的适用于 Android的新Apache HttpClient 包(如Apache建议的),它可能可以作为替代品使用。只需将以下依赖项添加到您的 build.gradle 文件(如果合适,更新版本号):

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

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

and replace import org.apache.http.*with import cz.msebera.android.httpclient.*everywhere in your project.

并替换import org.apache.http.*import cz.msebera.android.httpclient.*您项目中的任何地方。

Note that many classes from the old library are deprecated (e.g. HttpParams, ThreadSafeClientConnManager), so rewriting the code is probably going to be a better solution.

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



Edit:I found some cases where users were getting timeout exceptions when behind proxy servers after we updated to the newer Http client. Since the code is full of deprecated warnings everywhere, we decided that it wasn't worth the effort trying to fix the issue. I recommend testing very thoroughly before trying to put this into production.

编辑:我发现在我们更新到较新的 Http 客户端后,用户在代理服务器后面时会收到超时异常。由于代码到处都是已弃用的警告,因此我们认为不值得尝试修复该问题。我建议在尝试将其投入生产之前非常彻底地进行测试。

As mentioned in other answers, a much better solution is to bite the bullet and switch over to either the native Android HttpUrlConnection, or if that doesn't meet your needs, you can use the library OkHttp, which is what HttpUrlConnectionis internally based upon anyway.

正如其他答案中提到的,更好的解决方案是咬紧牙关切换到本机 Android HttpUrlConnection,或者如果这不能满足您的需求,您可以使用库OkHttp无论如何,这HttpUrlConnection内部基于的库。

回答by bfacumat

Add this to your gradle

将此添加到您的gradle

useLibrary 'org.apache.http.legacy'

Example

例子

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'
}

回答by Manas

HttpClient deperected from android lollypop 5.1 (API 22) :-
But still we can use HttpClient by using following code in Android studio:-
Goto app\build.gradle file:

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

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

        useLibrary 'org.apache.http.legacy'
    }

[OR ALTERNATIVE]

[或替代]


Download and add HttpClient jarfiles to your project or use okHttp.


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

回答by Sumukh Bhandarkar

Download the org.apache.http.legacyjar file from here.

org.apache.http.legacy这里下载jar 文件。

In the build.gradlefile, type the following code

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

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

Save the downloaded .jar file in the following location

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

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

I am using android-23 under platforms, it depends on which API level you are using. If the optional folder does not exist, create the optional folder and paste the .jar file in it before syncing the gradle project.

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

回答by Muzammil Husnain

Use Following Dependency in your Gradle

在 Gradle 中使用以下依赖项

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

回答by Matt Barrera

Add this to your gradle

将此添加到您的gradle

useLibrary 'org.apache.http.legacy'

Example

例子

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'
}

This library will allow you to use it without problem, at least I didn't had any problem till now with it.

这个库可以让你毫无问题地使用它,至少我到现在都没有遇到任何问题。