'How to import org.apache.httpcomponents using Gradle

Just trying to make some POST requests in an Android app. I was testing with HttpClient 4.5.3 (https://hc.apache.org/downloads.cgi) in a standalone script. Everything worked, but when I tried to add it to the Android app, I found out I need to add some repackaged dependency due to versioning issues. So I added

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'

in Gradle and synced it without issues. However, I can't import any of the classes I need to do what I need. These are the imports I need:

import org.apache.http.HttpEntity; // "HttpEntity" is highlighted red
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

What am I missing?



Solution 1:[1]

This works for me.

android {
    ... 
    useLibrary  'org.apache.http.legacy'
}
dependencies {
    ...
    compile 'org.apache.httpcomponents:httpcore:4.4.1'
    compile 'org.apache.httpcomponents:httpclient:4.5'
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 paradocslover