Android - MultipartEntity and dependencies
Asked Answered
L

4

18

I recently migrate my project from Eclipse to Android Studio (I do not fully control this IDE yet). In this project, I have a file uploader AsyncTask which send multipart over http. To do this, I use org.apache.httpcomponents. I created following dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'org.apache.httpcomponents:httpcore:4.4'
    compile 'org.apache.httpcomponents:httpmime:4.4'
    ...
}

In my java AsyncTask code:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody(...);
builder.addBinaryBody(...);
builder.addBinaryBody(...);
HttpEntity entity = builder.build(); // < throw exception

The exception I obtain at startup upload:

Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
    at org.apache.http.entity.ContentType.toString(ContentType.java:153)
    at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:52)
    at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:226)
    at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:230)

I think there is a conflict between android.jar > httpcomponents and the org.apache.httpcomponents dependency but I don't find a solution to resolve my problem.

Liar answered 12/2, 2015 at 6:8 Comment(0)
N
68

I had the same issue - try to use the http-android-client (this also relies on the httmime but requires 4.3). This worked for me:

dependencies {
  compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
  compile('org.apache.httpcomponents:httpmime:4.3') {
      exclude module: "httpclient"
  }
}
Northwestward answered 12/2, 2015 at 12:6 Comment(1)
@Northwestward Thanks for the answer it saved me.Working totally fine.Meal
T
11

Solution for Android Studio

Right click on app foleder in left- >Open Module Settings -> Dependencies-> click '+' -> 1. library Dependencie

now "choose file Dependencies" box will come - provide input 'httpmime:4.4' and click search button.

it will show the dependencie file. Select that file and click ok.

wait ... it will add the jar and Gradle script will be updated automatically.

Treed answered 25/2, 2015 at 6:25 Comment(0)
M
1
dependencies {
 compile('org.apache.httpcomponents:httpmime:4.3') {
    exclude module: "httpclient"
}
Mccay answered 10/11, 2016 at 8:40 Comment(0)
E
1
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
compile('org.apache.httpcomponents:httpmime:4.3') {
    // httpmime:4.2.1'  also worked 
    exclude module: "httpclient"
}
Evalynevan answered 24/12, 2016 at 4:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.