29

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'
joshkendrick
  • 3,497
  • 8
  • 38
  • 52
  • Are you using Proguard? – Emmanuel Dec 08 '14 at 20:37
  • 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 Dec 08 '14 at 20:46
  • 1
    use jarjar to repackage apache http library or use older httpmime library(chech which verion is compatble with minsdk of your project) – Selvin Dec 08 '14 at 20:59

6 Answers6

91

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'
ok2c
  • 26,450
  • 5
  • 63
  • 71
  • 4
    I was trying this approach with httpmim:4.5 and it wasn't working, but with 4.3.6 will (http://stackoverflow.com/a/29473284/1423773) don't forget that too. – GuilhE Jun 29 '15 at 13:43
  • what all other libraries are required?? because I am getting error - `Error:(76, 64) error: cannot access HttpEntity class file for org.apache.http.HttpEntity not found` I have used same libraries as mentioned in your reply. Additionally I was redirected from - [link]http://stackoverflow.com/questions/32796770/upload-image-via-volley-throwing-error[/link] – Jimit Patel Sep 28 '15 at 06:06
  • 1
    Ok i found out the solution, check here - http://stackoverflow.com/questions/32796770/upload-image-via-volley-throwing-error – Jimit Patel Sep 28 '15 at 10:34
  • @oleg [link](http://hc.apache.org/httpcomponents-client-4.3.x/android-port.html) how do i add it on eclipse?Also when i try to get jar - HttpClient for Android here [link](http://hc.apache.org/downloads.cgi) the link is dead,what do i do – Flying Monkey Dec 15 '15 at 12:26
  • Worked for me Thanks. – Rahul May 05 '16 at 06:07
  • Use 4.3.5.1 instead of 4.3.5 if you want to avoid some errors. – 0x777C May 30 '18 at 23:16
2

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

1

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

jazzyjester
  • 203
  • 1
  • 6
1

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)

Prakash
  • 71
  • 1
  • 2
0

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."

cmg_george
  • 309
  • 1
  • 2
  • 9
-1

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.