Home » Android » java – org.apache.http.entity.ContentType is not in Apache anymore?

java – org.apache.http.entity.ContentType is not in Apache anymore?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I can’t import org.apache.http.entity.ContentType for some reason. I have added Apache HTTP 4.3.3:

  • client
  • core
  • commons-codec
  • httpclient-cache
  • commons-logging
  • httpmime
  • fluent-hc

originally I didn’t import all of these, but since the problem persists I’ve done it all now, and I have cleaned my project and rebuilt it

import org.apache.http.entity.mime.MultipartEntityBuilder; works

and

import org.apache.http.HttpEntity; also works

but

import org.apache.http.entity.ContentType; is not found at all

I am using Android Studio (IntelliJ) so I’m not sure build path answers are helpful here

I also tried importing via gradle link, but I encountered a different error and need more control over the jar files myself

How to&Answers:

org.apache.http.entity.ContentType is in the org.apache.httpcomponents:httpcore:4.3.x module. But so is org.apache.http.HttpEntity. The fact you are finding one and not the other most likely means they are not being pulled in from the v4.3.x JAR. The Apache HttpCommponents project moved a lot of classes around recently. So it may be finding HttpEntity in an older/different version of httpcore that is getting pulled in somehow; a version that does not have the ContentType class. You’ll want to verify where it is finding HttpEntity. There are a few ways to see from where a dependency is getting pulled in from.

Option 1
One of the easiest ways is to put your cursor on the class (either in the import statement or a variable declaration) and open the quick documentation (Ctrl+Q or J). At the top of the documentation dialog, it will show the dependency where the class is found:

enter image description here

Option 2
This option will show you if you have multiple instances of a class on your class path. Open the Goto Class dialog (Ctrl+N or O) and enter the class name (you can fully qualify or not. You can even paritally enter the name and use camel case search). In the list of classes that are found, to the right will be where the class is found.
enter image description here
TO the right is the dependency from which the class is coming from. If it shows more than once, it means that it is being pulled in multiple times in different jars. This can happen (when using a build tool) if another dependency is pulling in a different version of httpcore as a transitive dependency. So you will need to resolve that. Notice in my screen shot I have two different versions of the org.springframework.http.HttpEntity class. One from Spring 3.2.10 and one from 4.0.6. In this particular case it is because I have a Spring 3.2.x based module and a separate Spring 4.0.x module in my project. So I’m OK with that as they do not clash since those are independent modules. But in most cases, that would be cause for concern.

Option 3
You can also expand and look at the External Libraries node in the Project Tool window to see if a dependency is being pulled in twice.

It’s unclear how your project is configured. You mentioned gradle, but it seemed more like an after thought. If you are using maven or gradle, and the proper httpcore dependency is declared in your pom or build file, make sure you perform a reimport enter image description here on the appropriate build tool window so that it synchronizes properly and that module is added to the project’s dependency. Then use the above information to see if you have multiple versions of the httpcore module being pulled in.

Answer:

I had the same issue. You have to make sure you are using jdk 1.7 and not 1.6.

Answer:

Download httpcore-4.3.2 jar and add to lib folder.