Android Gradle Apache Httpclient Does Not Exist

Android Gradle Apache HttpClient does not exist?

I suggest you replace the deprecated apache HttpClient with the new HttpURLConnection.

That's a cleaner solution, it's quite easy to migrate, and generally it's better to stick to the latest SDK changes than trying to hack/patch/workaround: you usually regret it later :)

Step 1

HttpGet httpGet = new HttpGet(url);

becomes:

URL urlObj = new URL(url);

Step 2

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpGet, localContext);
InputStream is = response.getEntity().getContent();

becomes:

HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
InputStream is = urlConnection.getInputStream();

Step 2 bis

int status = response.getStatusLine().getStatusCode();

becomes:

int status = urlConnection.getResponseCode();

HttpClient won't import in Android Studio

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

package org.apache.http.client does not exist

Add that to your build.gradle:

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

Or you use the HttpURLConnection class instead.

Apache HttpClient Android (Gradle)

If you are using target SDK as 23 add the below code in your build.gradle

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

Additional note here: don't try using the gradle versions of those files. They are broken (28.08.15). I tried over 5 hours to get it to work. It just doesn't.
not working:

compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'

Another thing don't use:

'org.apache.httpcomponents:httpclient-android:4.3.5.1'

It's referring to 21 API level.

How do I properly import HttpClient from org.apache on Android using gradle build file?

I think the httpclient library doesn't include the mime parts, those are in httpmime. This is a transitive dependency of httpclient, but as that is ignored, it won't be taken into account.

Try adding this dependency:

compile "org.apache.httpcomponents:httpmime:4.2.3"


Related Topics



Leave a reply



Submit