HttpClient won't import in Android Studio
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?
Solution 1:
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
Solution 2:
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'
}
Solution 3:
TejaDroid's answer in below link helped me . Can't import org.apache.http.HttpResponse in Android Studio
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'
...
}