2

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?

Jerodev
  • 32,252
  • 11
  • 87
  • 108
Nadir Novruzov
  • 467
  • 1
  • 6
  • 16

2 Answers2

5

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);
Kampai
  • 22,848
  • 21
  • 95
  • 95
NashLegend
  • 51
  • 1
  • 3
0

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);  
RobertS
  • 135
  • 1
  • 11