I am trying to convert an IntelliJ project to the Gradle system of Android Studio but I am running into errors with Apache HttpClient? Am I missing something, the errors I am getting are as follows:
Error:(10, 30) error: package org.apache.http.client does not exist
Error:(11, 30) error: package org.apache.http.client does not exist
Error:(12, 37) error: package org.apache.http.client.entity does not exist
Error:(13, 38) error: package org.apache.http.client.methods does not exist
Error:(14, 38) error: package org.apache.http.client.methods does not exist
Error:(15, 38) error: package org.apache.http.client.methods does not exist
Error:(16, 35) error: package org.apache.http.impl.client does not exist
Error:(134, 33) error: cannot find symbol class HttpUriRequest
Error:(164, 39) error: cannot find symbol class HttpUriRequest
Error:(106, 17) error: cannot find symbol class HttpGet
Error:(106, 39) error: cannot find symbol class HttpGet
Error:(117, 17) error: cannot find symbol class HttpPost
Error:(117, 40) error: cannot find symbol class HttpPost
Error:(125, 43) error: cannot find symbol class UrlEncodedFormEntity
Error:(135, 9) error: cannot find symbol class HttpClient
Error:(135, 33) error: cannot find symbol class DefaultHttpClient
Error:(155, 18) error: cannot find symbol class ClientProtocolException
Error:(165, 9) error: cannot find symbol class HttpClient
Error:(165, 33) error: cannot find symbol class DefaultHttpClient
Error:(185, 18) error: cannot find symbol class ClientProtocolException
My build.gradle file has the following dependencies:
dependencies {
compile 'com.google.android.gms:play-services:+'
compile 'org.apache.httpcomponents:httpclient:4.2.6'
compile 'org.apache.httpcomponents:httpmime:4.2.6'
compile files('libs/core.jar')
}
It seems a lot of people are getting a similar problem but neither SO or Google have a solution so I am hoping this question will help future searchers.
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();