android - replacement - org.apache.http.httpentity package




HttpClient won't import in Android Studio (15)

Another way is if you have httpclient.jar file then you can do this :

Paste your .jar file in "libs folder" in your project. Then in gradle add this line in your build.gradle(Module:app)

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile files('libs/httpcore-4.3.3.jar')
}

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?


1- download Apache jar files (as of this answer) 4.5.zip file from:
https://hc.apache.org/downloads.cgi?Preferred=http%3A%2F%2Fapache.arvixe.com%2F

2- open the zip copy the jar files into your libs folder. You can find it if you go to the top of your project where it says "Android" you'll find a list when u click it. So,

Android -> Project -> app -> libs

,Then put jars there.

3- In build.gradle (Module: app) add

compile fileTree(dir: 'libs', include: ['*.jar'])

in

 dependency { 
   }

4- In the java class add these imports:

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.CoreProtocolPNames;

Android 6.0 (API Level 23) release removes support for the Apache HTTP client. Hence you can not use this library directly in API 23. But there is a way to use it. Add useLibrary ‘org.apache.http.legacy’ in your build.gradle file as below-

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

If this does not work you may apply the following hack-

– Copy org.apache.http.legacy.jar which is in /platforms/android-23/optional path of your Android SDK directory to your project’s app/libs folder.

– Now Add compile files(‘libs/org.apache.http.legacy.jar’) inside dependencies{} section of build.gradle file.


ApacheHttp Client is removed in v23 sdk. You can use HttpURLConnection or third party Http Client like OkHttp.

ref : https://developer.android.com/preview/behavior-changes.html#behavior-apache-http-client


Error:(30, 0) Gradle DSL method not found: 'classpath()' Possible causes:

  • The project 'cid' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0). Upgrade plugin to version 2.3.3 and sync project
  • The project 'cid' may be using a version of Gradle that does not contain the method. Open Gradle wrapper file
  • The build file may be missing a Gradle plugin. Apply Gradle plugin

  • HttpClient is not supported any more in sdk 23. Android 6.0 (API Level 23) release removes support for the Apache HTTP client. You have to use

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

    and also add below code snippet in your dependency :

    //http final solution for web-service (including file uploading)

    compile('org.apache.httpcomponents:httpmime:4.3.6') {
            exclude module: 'httpclient'
    }
     compile 'org.apache.httpcomponents:httpclient-android:4.3.5'

    It will also help you while you use Use MultipartEntity for File upload .


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

    I think depending on which Android Studio version you have, it's important you update your android studio as well, i was becoming frustrated too following everyone's advice but no luck, until i had to upgrade my android version from 1.3 to 1.5, the errors disappeared like magic.


    If you want import some class like :

    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.params.BasicHttpParams;
    import org.apache.http.params.HttpConnectionParams;
    import org.apache.http.params.HttpParams;

    You can add the following line in the build.gradle (Gradle dependencies)

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:27.1.0'
        implementation 'com.android.support:support-v4:27.1.0'
    
        .
        .
        .
    
        implementation 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    
    }

    Simply use this :-

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

    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:

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

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

    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'

    You can simply add this to Gradle dependencies:

    compile "org.apache.httpcomponents:httpcore:4.3.2"

    You have to add just one line

    useLibrary 'org.apache.http.legacy'

    into build.gradle(Module: app), for example

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 24
        buildToolsVersion "25.0.0"
    
        useLibrary 'org.apache.http.legacy'
    
        defaultConfig {
            applicationId "com.avenues.lib.testotpappnew"
            minSdkVersion 15
            targetSdkVersion 24
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:24.2.1'
        testCompile 'junit:junit:4.12'
    }

    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





    apache-httpclient-4.x