'org.apache.http.HttpEntity' is deprecated. How to solve this error in android studio?
Add this to your gradle
useLibrary 'org.apache.http.legacy'
Example
android {compileSdkVersion 23buildToolsVersion "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'}
Quoting myself:
If you need to continue using the HttpClient API, consider switching to OkHttp and their HttpClient compatibility layer, or consider switching to Apache’s separate Android edition of HttpClient. Otherwise, switch to HttpURLConnection or OkHttp’s native 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.).
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.
The version of the Apache HTTP client provided on stock Android was very very old.
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.
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.
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):
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.
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.
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.
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 HttpUrlConnection
is internally based upon anyway.