14

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

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

yole
  • 92,896
  • 20
  • 260
  • 197
Sml
  • 143
  • 2
  • 2
  • 5
  • 2
    It is not an error. It is warning. You should read the documentation. There must be a hint which class you should use – Jens May 31 '15 at 10:59
  • inspection 'Deprecated API usage' options: edit inspection profile setting, and Annotate class 'HttpEntity' as @Deprecated: edit intention settings. which options should i go for?? i really have no idea what they mean – Sml May 31 '15 at 11:14

7 Answers7

20

Add this to your 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'
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
bfacumat
  • 331
  • 2
  • 5
  • The msebera version is 1.1mb, quite an overhead for a small app. The legacy version is 297k. okhttp + compatibility jars total 382kb - and would encourage switching over to a modern API - but the compatibility layer doesn't cover enough classes for me. – NeilS Oct 27 '15 at 13:15
7

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • is there a simple replacement for those classes in API? – Sml May 31 '15 at 11:26
  • @lamriassia: I have no idea what you would consider "simple" and what you consider "in API" to be. I have listed several replacements in my answer (OkHttp's HttpClient API, Apache's replacement HttpClient, using OkHttp directly, HttpUrlConnection, Retrofit, Picasso, and Universal Image Loader). You will have to decide for yourself which, if any, of those are "simple" and which, if any, are "in API". – CommonsWare May 31 '15 at 11:27
  • @CommonsWare what have you used finally. Can you share the code that you have written. – Sagar Patil Jun 24 '15 at 10:51
  • How come "useLibrary 'org.apache.http.legacy'" doesn't help with using the old APIs ? – android developer Jul 27 '15 at 11:04
  • @androiddeveloper: I moved off of HttpClient a year or two ago; I have not attempted to use the `.legacy` stuff in the M Developer Preview. – CommonsWare Jul 27 '15 at 11:19
  • I know it's better to leave it, but we need to check if the app can work fine with the rest of the stuff . – android developer Jul 27 '15 at 11:32
5

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.

Tim Rae
  • 3,167
  • 2
  • 28
  • 35
  • 2
    Here we are, December 2015, and after adding `cz.msebera.android.httpclient 4.4.1.1` in my app build.gradle, Android Studio 1.5, sdk 23, tells that `HttpParams, HttpConnectionParams, BasicHttpParams, HttpProtocolParams` and `DefaultHttpClient` are deprecated. It seems I'll have to recode everything from scratch. – Junior Mayhé Dec 05 '15 at 15:55
4

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:

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

        useLibrary 'org.apache.http.legacy'
    }

[OR ALTERNATIVE]


Download and add HttpClient jar files to your project or use okHttp.

Manas
  • 41
  • 2
1

Use Following Dependency in your Gradle

dependencies {
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}
Rob
  • 26,989
  • 16
  • 82
  • 98
Muzammil Husnain
  • 1,218
  • 1
  • 10
  • 24
0

Download the org.apache.http.legacy jar file from here.

In the build.gradle file, type the following code

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

Save the downloaded .jar file in the following location

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

Sumukh Bhandarkar
  • 386
  • 1
  • 5
  • 14
0

Add this to your 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.

Matt Barrera
  • 149
  • 1
  • 8