NoClassDefFoundError in MultipartEntityBuilder while uploading file

6.5k Views Asked by At

I want to upload image and send data to server. I am using MultipartEntityBuilder for this. I am coding on Android Studio.

here is my code

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(config.api_url+"profile.php");

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

if(mLastTakenImageAsJPEGFile!=null) {
    builder.addBinaryBody("image", mLastTakenImageAsJPEGFile);
}

builder.addTextBody("token", _appPrefs.getToken());
final HttpEntity reqEntity = builder.build();

httpPost.setEntity(reqEntity);

I am getting Runtime Error

java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType 
at 
org.apache.http.entity.mime.MultipartEntityBuilder.addBinaryBody(MultipartEntityBuilder.java:146)

How to solve this?

2

There are 2 best solutions below

0
On

I found this problem with httpmime 4.3.6, and i haven't found a solution, I suggest you use httpmime 4.2.1, it works fine.

MultipartEntity multipartEntity = new MultipartEntity();
ContentBody contentBody = new FileBody(file);
multipartEntity.addPart("image", new FileBody(file));
httpPost.setEntity(multipartEntity);
0
On

I had the same issue,

you need to add httpcore-4.2.3.jar and httpmime-4.3.2.jar and if you using IOutils, use ByteStreams guava-18.jar don´t forget internet permission and add .jar to your \projectName\app\libs folder and sync as well.

Use this:

byte[] data;
data = ByteStreams.toByteArray(inputStream); 

Instead of:

byte[] data;
data = IOUtils.toByteArray(inputStream);