Entity returns error in MultiPartEntityBuilder
Asked Answered
I

3

4

I've tried following links but none of them helped to solve the issue.

HttpPost returning error when using MultipartEntityBuilder in Android

https://mcmap.net/q/242040/-post-multipart-request-with-android-sdk

Here's the code

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Utility.AddProductWS);

MultipartEntityBuilder multipartBuilder = MultipartEntityBuilder.create();

/* example for setting a HttpMultipartMode */
multipartBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

for (int i = 0; i < photos.size(); i++) {

    multipartBuilder.addBinaryBody("images[]", newFile(photos.get(i).getImagePath());
}

multipartBuilder.addTextBody("username", username.toString());
multipartBuilder.addTextBody("accept_best_offer", String.valueOf(acceptOffer.isChecked() ? 1 : 0));
multipartBuilder.addTextBody("accept_trade", String.valueOf(acceptTrade.isChecked() ? 1 : 0));
multipartBuilder.addTextBody("product_price", etProductPrice.getText().toString());
multipartBuilder.addTextBody("product_description", etProductDescription.getText().toString());

HttpEntity httpEntity = multipartBuilder.build();
httpPost.setEntity(httpEntity);  // Error line
HttpResponse response = httpClient.execute(httpPost);
Utility.showLog(TAG, EntityUtils.toString(response.getEntity()));

Error

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

I am using httpcore-4.4.1 and httpmime-4.4.1 library files.

Inconveniency answered 7/5, 2015 at 21:12 Comment(5)
try to add post one file using entityBuilder.addBinaryBody(IMAGE, file); and check.Hyrcania
I tried the same and it doesnt work too :(Inconveniency
@MuhammadBabar any idea?Inconveniency
@MuhammadBabar Finally it worked!! It's a library problem. Thanks for looking the issue.Inconveniency
Glad to heard you're done :)Hyrcania
I
14

Finally I've solved it.

It's a library issue, it worked with 4.3.1 version.

Here's the link to download library files

Inconveniency answered 11/5, 2015 at 21:7 Comment(2)
@droid_dev: I have these versions but it doesn't work (same error like you): compile('org.apache.httpcomponents:httpmime:4.3.1') { exclude module: "httpclient" } compile('org.apache.httpcomponents:httpcore:4.3.1') { exclude module: "httpclient" }Sadie
Now try with okhttp library. It works without these libraryInconveniency
C
3

Try with theese versions, is working for me

compile 'org.apache.httpcomponents:httpcore:4.3.3'
compile('org.apache.httpcomponents:httpmime:4.3.6') {
    exclude module: 'httpclient'
}

Also you can read more here.

Carhart answered 29/11, 2017 at 16:37 Comment(0)
I
0

multipartBuilder.addPart("images[]", fileBody); fileBody convert into byteArray then you pass because data travel in byte array

Try to upload the image in php server but it could post. in android

Immingle answered 8/5, 2015 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.