HttpClient won't import in Android Studio

后端 未结 23 2436
一生所求
一生所求 2020-11-22 14:44

I have a simple class written in Android Studio:

package com.mysite.myapp;

import org.apache.http.client.HttpClient;

public class Whatever {
    public voi         


        
相关标签:
23条回答
  • 2020-11-22 15:11

    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'
    }
    
    0 讨论(0)
  • 2020-11-22 15:12

    Which API target do you have within your project?AndroidHttpClientis only for API Level 8 <. and please have a look on here

    enjoy your code:)

    0 讨论(0)
  • 2020-11-22 15:13

    You can simply add this to Gradle dependencies:

    compile "org.apache.httpcomponents:httpcore:4.3.2"
    
    0 讨论(0)
  • 2020-11-22 15:14

    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.

    0 讨论(0)
  • 2020-11-22 15:15

    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;
    
    0 讨论(0)
  • 2020-11-22 15:15

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

    android {
        useLibrary 'org.apache.http.legacy'
    }
    
    0 讨论(0)
提交回复
热议问题