Can't import org.apache.http.HttpResponse in Android Studio

后端 未结 6 1786
醉梦人生
醉梦人生 2020-12-03 06:53

I want to use these libraries in Android Studio:

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client         


        
相关标签:
6条回答
  • 2020-12-03 07:11

    HttpClient is deprecated in sdk 23.

    You have to move on URLConnection or down sdk to 22

    Still you need HttpClient with update gradle sdk 23

    You have to add the dependencies of HttpClient in app/gradle as

    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'
        ...
    }
    
    0 讨论(0)
  • 2020-12-03 07:25

    Main build.gradle - /build.gradle

    buildscript {
        ...
        dependencies {
            classpath 'com.android.tools.build:gradle:1.3.1' 
            // Versions: http://jcenter.bintray.com/com/android/tools/build/gradle/
        }
        ...
    }
    

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

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.1"
        ...
        useLibrary 'org.apache.http.legacy'
        ...
    }
    
    0 讨论(0)
  • 2020-12-03 07:31

    HttpClient was deprecated in Android 5.1 and is removed from the Android SDK in Android 6.0. While there is a workaround to continue using HttpClient in Android 6.0 with Android Studio, you really need to move to something else. That "something else" could be:

    • the built-in classic Java HttpUrlConnection
    • Apache's independent packaging of HttpClient for Android
    • OkHttp (my recommendation)
    • AndroidAsync

    Or, depending upon the nature of your HTTP work, you might choose a library that supports higher-order operations (e.g., Retrofit for Web service APIs).

    In a pinch, you could enable the legacy APIs, by having useLibrary 'org.apache.http.legacy' in your android closure in your module's build.gradle file. However, Google has been advising people for years to stop using Android's built-in HttpClient, and so at most, this should be a stop-gap move, while you work on a more permanent shift to another API.

    0 讨论(0)
  • 2020-12-03 07:33

    According to the Apache site this is the Gradle dependency you need to include, if you use Android API 23 or newer:

    dependencies {
        compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
    }
    

    Source: https://hc.apache.org/httpcomponents-client-4.5.x/android-port.html

    0 讨论(0)
  • 2020-12-03 07:35

    Use This:-

    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    
    0 讨论(0)
  • 2020-12-03 07:38

    in case you are going to start development, go fot OkHttp from square, otherwise if you need to keep your previous code running, then add legacy library to your project dependencies:

    dependencies {
        compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
    }
    
    0 讨论(0)
提交回复
热议问题