Including Apache HTTP Client on Android Studio


 

Android Studio - Apache HTTP Client

In order to use Apache HTTP Client (httpclient y httpmime) in Eclipse I used to download the Android port and then include the dependencies specifying the jar files: /home/lgallard//Android/libs/httpcore-4.3.2.jar /home/lgallard//Android/libs/httpmime-4.3.5.jar I kept this practice after migrating my projects to Android Studio, but I used Gradle to resolve the remaining libraries. Then to uniform everything, I did a research on how to include them. Just add the following lines to your app’s build.gradle file:


apply plugin: 'com.android.application'

android {

    dependencies {
        compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
        compile (group: 'org.apache.httpcomponents' , name: 'httpmime' , version: '4.3.5') {
        exclude module: 'org.apache.httpcomponents:httpclient'}
    }

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

}

Then sync gradle and compile your project again.

Reference: Apache HttpClient Android (Gradle)


,

  1. #1 by Luis Gallardo on 03/09/2016 - 2:47 pm

    @Marco actualicé la información. Debes colocar lo siguiente en tu archivo gradle:


    apply plugin: 'com.android.application'

    android {

    dependencies {
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
    compile (group: 'org.apache.httpcomponents' , name: 'httpmime' , version: '4.3.5') {
    exclude module: 'org.apache.httpcomponents:httpclient'}
    }

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

    }

    Saludos,

  2. #2 by Luis Gallardo on 03/09/2016 - 2:30 pm

    @Raja add the following in your gradle file:

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

    Regards!

  3. #3 by Raja on 05/08/2016 - 9:53 pm

    Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.3.5 is ignored for release as it may be conflicting with the internal version provided by Android.
    In case of problem, please repackage it with jarjar to change the class packages

    How to solved

  4. #4 by Raja on 05/08/2016 - 9:52 pm

    I am using android compileSdkVersion 24 Error Dependency org.apache.httpcomponents:httpclient:4.4.1 is ignored for release as it may be conflicting with the internal version provided by Android occured…

  5. #5 by Marco Antonio on 14/07/2016 - 11:09 am

    Error:(9, 1) A problem occurred evaluating root project ‘TiendaOnline’.
    > Could not find method compile() for arguments [{group=org.apache.httpcomponents, name=httpclient-android, version=4.3.5.1}] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@56d48cb5.

    ya ise lo que indicas en la practica.. al finalizar y sincronizar el proyecto marca el error anterior…estoy usando la ultima version de android…. ayuda por favor

  6. #6 by Marco Antonio on 14/07/2016 - 11:05 am

    Error:(9, 1) A problem occurred evaluating root project ‘TiendaOnline’.
    > Could not find method compile() for arguments [{group=org.apache.httpcomponents, name=httpclient-android, version=4.3.5.1}] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@56d48cb5.

    ya ise lo que indicas en la practica… y estoy trabajando con la ultima versión de android

  7. #7 by Luis Gallardo on 02/06/2016 - 2:45 pm

    @Sheikh you are welcome. Regards!

  8. #8 by Sheikh on 29/05/2016 - 2:38 pm

    Thankx…. Really work

  9. #9 by Luis Gallardo on 29/04/2016 - 1:42 pm

    @Roxii Debería ser suficiente con la declaración:

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

    Por otro lado procura actualizar Android Studio a su última versión. Saludos!

  10. #10 by Roxii on 07/04/2016 - 1:24 pm

    Hola! mi duda es
    he importado

    android {
    useLibrary ‘org.apache.http.legacy’
    }

    dependencies {
    compile fileTree(dir: ‘libs’, include: [‘*.jar’])
    compile files(‘libs/your jar file.jar’)
    testCompile ‘junit:junit:4.12’

    compile ‘com.android.support:appcompat-v7:23.1.1’

    compile ‘com.android.support:design:23.1.1’

    compile ‘com.google.android.gms:play-services:8.4.0’

    compile ‘org.apache.httpcomponents:httpcore:4.4.1’
    compile ‘org.apache.httpcomponents:httpclient:4.5’

    }

    pero no me realiza ningun cambio, estoy intentando parsear datos en JSON

    y en el debug me lanza

    Error:(5, 23) error: cannot find symbol class HttpEntity
    Error:(6, 23) error: cannot find symbol class HttpResponse
    Error:(7, 30) error: cannot find symbol class ClientProtocolException
    Error:(8, 37) error: cannot find symbol class UrlEncodedFormEntity
    Error:(9, 38) error: cannot find symbol class HttpGet
    Error:(10, 38) error: cannot find symbol class HttpPost
    Error:(11, 36) error: cannot find symbol class URLEncodedUtils
    Error:(12, 35) error: cannot find symbol class DefaultHttpClient
    Error:(45, 17) error: cannot find symbol class DefaultHttpClient
    Error:(45, 52) error: cannot find symbol class DefaultHttpClient
    Error:(46, 17) error: cannot find symbol class HttpPost
    Error:(46, 41) error: cannot find symbol class HttpPost
    Error:(47, 40) error: cannot find symbol class UrlEncodedFormEntity
    Error:(49, 17) error: cannot find symbol class HttpResponse
    Error:(50, 17) error: cannot find symbol class HttpEntity
    Error:(55, 17) error: cannot find symbol class DefaultHttpClient
    Error:(55, 52) error: cannot find symbol class DefaultHttpClient
    Error:(56, 38) error: cannot find symbol variable URLEncodedUtils
    Error:(58, 17) error: cannot find symbol class HttpGet
    Error:(58, 39) error: cannot find symbol class HttpGet
    Error:(60, 17) error: cannot find symbol class HttpResponse
    Error:(61, 17) error: cannot find symbol class HttpEntity
    Error:(68, 18) error: cannot find symbol class ClientProtocolException

    ¿Me podrías ayudar?

  11. #11 by Luis Gallardo on 14/11/2015 - 10:04 am

    @Rodolfo hola, recuerda que HTTP Client ahora esta “deprecated”. Por eso el error. Saludos!

  12. #12 by Rodolfo on 05/11/2015 - 9:25 am

    Lo realice en una computadora recien formateada y me funciono sin problema, no se a que se deba que en ocasiones marca el error que a todos les marca, de cualquier manera muchas gracias por el aporte y puedo recomendar un curso de android http://www.grupocodesi.com/cursos/curso-de-android.html para las personas que inician

  13. #13 by Luis Gallardo on 02/11/2015 - 11:02 pm

    @Diego bastaría con colocar las dependencias que comento en el artículo. Al menos hasta el SDK 22. Apartir del 23 debes incluir esto a tu gradle:

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

    Saludos

  14. #14 by Diego G on 31/10/2015 - 5:14 pm

    luis :
    Tengo el mismo problema, sigue sin importar las librerías:
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URLEncodedUtils;
    import org.apache.http.impl.client.DefaultHttpClient;

    Por ejemplo sólo dice: Cannot resolve symbol ‘HttpEntity’

  15. #15 by Luis Gallardo on 08/10/2015 - 12:14 am

    @luis exactamente qué estás haciendo? Algún error?

    Saludos!

  16. #16 by luis on 29/09/2015 - 2:52 pm

    Lo estoy tratando de ser asi pero me sigue sin importar las librerias del apache alguna sugerencia?

(will not be published)