29
votes

I'm working on an app on Android. I'm using httpcore 4.3.3. I get this when I try to use ContentType.parse(string)

java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicHeaderValueParser; in class Lorg/apache/http/message/BasicHeaderValueParser; or its superclasses (declaration of 'org.apache.http.message.BasicHeaderValueParser' appears in /system/framework/ext.jar)

I've done some googling and I understand why I'm getting the error, but I'm unsure how to fix it. From what I've read, it seems that ContentType tries to make use of the BasicHeaderValueParser that comes with Android and that class doesn't have the INSTANCE field yet. Any help please?

These are the relevant imports:

compile('org.apache.httpcomponents:httpmime:4.3.6') {
    exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpcore:4.3.3'
6
Are you using Proguard? - Emmanuel
i dont think so. i recently upgraded to android studio 1.0 which makes you use minifyEnabled instead of runProguard, i think i have them set to false. i can try setting them to true, but is there another way to fix this? --> didnt seem to fix it - joshkendrick
use jarjar to repackage apache http library or use older httpmime library(chech which verion is compatble with minsdk of your project) - Selvin

6 Answers

91
votes

Unfortunately stock versions of HttpCore cannot be used in Android. Please use Apache HttpClient Android Port which also includes requisite HttpCore classes

compile('org.apache.httpcomponents:httpmime:4.3.6') {
    exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
2
votes

Download latest Apache Httpclient Jar file and refract path name. That works for me.

1
votes

Looks like you use incompatible jar's for httpmime and httpclient-android. Try to use version 4.3.6 of httpmime

1
votes

Just add httpmime_4.3.6.jar of version 4.3.6 file in lib folder and add

implementation files('lib/httpmime-4.3.6.jar') in gradle(app level)

0
votes

Simple solution is to use:

https://code.google.com/p/httpclientandroidlib/

It's the Appache HttpClient port for android wrap in a different package name so that will not interfere with the existing version in Android. Import the jar in the Android project and instead of "org.apache.http" use "ch.boye.httpclientandroidlib."

-1
votes

Note : If you are using both apache httpmime and retrofit simultaneously Use apache httpmime older version.

(eg : in my case i used this to upload a image on server and also to get the content lenghth/image size)

This his an older version (not so old) or prev version, You should implement same old version for both.

implementation 'com.squareup.retrofit2:retrofit:2.0.2' implementation 'org.apache.httpcomponents:httpmime:4.3.6'

This his an new version of retrofit, You should use old version apache httpmime.

implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'org.apache.httpcomponents:httpmime:4.3.6'

Thank You so much, If u loved my solution plz voteup for me.