HttpClient won't import in Android Studio

HttpClient won't import in Android Studio

主要原因是 HttpClient 在SDK23中将不再被支持;
建议的解决方案为:
1.更换为使用 URLConnection
2.或者将SDK版本降低到22;( compile 'com.android.support:appcompat-v7:22.2.0' )
3.仍然要使用SDK23,并在之上使用HttpClient的话需要在gradle中添加
android { useLibrary 'org.apache.http.legacy' }

原文请见下方: 


up vote 70 down vote favorite
19

I'm crawling out of my skin. I have the world's simplest class:

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 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? Thanks!

share | improve this question
 
3  
Try to use AndroidHttpClient if you can. HttpClient stubs are indeed contained inside the android jar, so there should be no need to refer to it explicitly. Note that the android version of httpclient is probably 4.1.1. Trying to use a newer version on top of that is usually asking for trouble (read: doesn't work, because the firmware classloader always wins). –  dhke Aug 22 at 7:06

9 Answers 9

active oldest votes
up vote 150 down vote accepted

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

share | improve this answer
 
1  
Announcement link: developer.android.com/preview/… –  dhke Aug 22 at 7:15
5  
It doesn't do anything in my case. How come? –  android developer Sep 3 at 12:04
1  
useLibrary cannot be resolved when I add it. My target is 23, what i'm I missing? –  fullMoon Sep 8 at 11:05
 
@androiddeveloper, I was the same. See my answer below. –  fullMoon Sep 8 at 19:41
 
They use this in license vending code. Have they upgraded this for M also? –  powder366 Sep 11 at 15:00
up vote 35 down vote

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'
}
share | improve this answer
 
 
This should be the accepted answer. –  Kevin Krumwiede Aug 23 at 6:06
 
easy and fast solution if someone does not mind using a deprecated class... –  prom85 Sep 1 at 18:25
2  
android { useLibrary 'org.apache.http.legacy' } [when u add this] [1] 'org.apache.http' android-studio still have tips:'Cannot resolve symbol' [2] this make android-studio(with sdk23) can build. though there have tips(Cannot resolve symbol) [3] if u add jar,it no tips,can build,but!!!cannot run,because it has two copy 'org.apache.http' –  YETI Sep 16 at 7:44
1  
[2.1] So close Android Studio and start it again, don't open the file(s) that make use of the legacy code and the "tips" won't be applied. If you need to open the file then it suggests you own the code, so [4] you should stop using HttpClient in code you control. –  straya Sep 16 at 23:27
 
Donot know why (build-tool&sdk)23's behavious so freak. Event donot support developer use 'apche http' on one's mind. –  YETI Sep 17 at 3:20

你可能感兴趣的:(HttpClient won't import in Android Studio)