HttpClient won't import in Android Studio

601,962

Solution 1

HttpClient is not supported any more in sdk 23. You have to use URLConnection or downgrade to sdk 22 (compile 'com.android.support:appcompat-v7:22.2.0')

If you need sdk 23, add this to your gradle:

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

You also may try to download and include HttpClient jar directly into your project or use OkHttp instead

Solution 2

HttpClient was deprecated in API Level 22 and removed in API Level 23. You can still use it in API Level 23 and onwards if you must, however it is best to move to supported methods to handle HTTP. So, if you're compiling with 23, add this in your build.gradle:

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

Solution 3

TejaDroid's answer in below link helped me . Can't import org.apache.http.HttpResponse in Android Studio

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'

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

Solution 4

To use Apache HTTP for SDK Level 23:

Top level build.gradle - /build.gradle

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0' 
        // Lowest version for useLibrary is 1.3.0
        // Android Studio will notify you about the latest stable version
        // See all versions: http://jcenter.bintray.com/com/android/tools/build/gradle/
    }
    ...
}

Notification from Android studio about gradle update:

Notification from Android studio about gradle update

Module specific build.gradle - /app/build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    ...
    useLibrary 'org.apache.http.legacy'
    ...
}

Solution 5

Try this worked for me Add this dependency to your build.gradle File

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
Share:
601,962
AndroidDev
Author by

AndroidDev

Updated on April 06, 2021

Comments

  • AndroidDev
    AndroidDev about 3 years

    I have a simple class written in Android Studio:

    package com.mysite.myapp;
    
    import org.apache.http.client.HttpClient;
    
    public class Whatever {
        public void headBangingAgainstTheWallExample () {
            HttpClient client = new DefaultHttpClient();
        }
    }
    

    and from this I get the following compile time error:

    Cannot resolve symbol HttpClient

    Isn't HttpClient included in the Android Studio SDK? Even if it is not, I added it to my Gradle build like this:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.0'
        compile 'org.apache.httpcomponents:httpclient:4.5'
    }
    

    With or without the last compile line, the error is the same. What am I missing?