简体   繁体   中英

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?

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'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM