1

I'm trying to use HttpPost as I did in the past in Android. I realize that it has been removed from recent Android versions. Following the solutions on a similar StackOverflow question, I added this to my Gradle dependencies:

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

After a Gradle sync that complained of no errors, I still cannot import the HttpPost program. In fact, the org.apache.http.client.methods package does not resolve at all.

I also tried what the Apache HttpClient website suggested, which was to use this instead in my Gradle dependencies:

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

Once again, the sync went fine, and this time the package resolved, but not the HttpPost program.

enter image description here

I'd be grateful for any suggestions.

Casey Perkins
  • 1,853
  • 2
  • 23
  • 41

2 Answers2

5

Just add this line in your build.gradle inside android plugin :

useLibrary 'org.apache.http.legacy'
Yurii Tsap
  • 3,554
  • 3
  • 24
  • 33
  • 1
    But these are old versions of the Apache clients, yes? Wouldn't it be better to have the newer versions? Is there a way to do that? – Casey Perkins Apr 21 '16 at 21:23
  • The answer above is quite full and describes the whole problem, in API 23 this client was fully removed and became just a legacy. I think it's better to use HttpOk, Retrofit or simply UrlConnection. – Yurii Tsap Apr 21 '16 at 21:26
3

The Apache HttpClient 4.3 for Android meant to be an update for the legacy 4.0 alpha version that was included in early Android versions. To avoid naming conflicts some on the updated classes got the postfix HC4, like HttpPostHC4 for example, which one had to use instead.

But now that the old HttpClient was removed form Android, the old classes like HttpPost do not exist anymore.

The official way to get the HttpClient library on newer Android version is to use:

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

as documented here.

Community
  • 1
  • 1
Floern
  • 33,559
  • 24
  • 104
  • 119