Sml
Sml

Reputation: 143

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

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?

Upvotes: 14

Views: 61125

Answers (7)

Matt Barrera
Matt Barrera

Reputation: 159

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.

Upvotes: 0

Muzammil Husnain
Muzammil Husnain

Reputation: 1258

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

Upvotes: 1

Sumukh Bhandarkar
Sumukh Bhandarkar

Reputation: 384

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.

Upvotes: 0

Manas
Manas

Reputation: 41

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.

Upvotes: 4

Tim Rae
Tim Rae

Reputation: 3186

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.

Upvotes: 5

bfacumat
bfacumat

Reputation: 341

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

Upvotes: 21

CommonsWare
CommonsWare

Reputation: 1006674

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.

Upvotes: 7

Related Questions