Android post image to the Server using MultipartEntity

Geetanjali picture Geetanjali · Nov 2, 2012 · Viewed 11.7k times · Source

I have been trying to upload an image and data to Django server. I have included apache-mime4j.0.6.jar and httpmime4.0.1.jar libraries ( Project->build path->Add external jar files) And here's the code to upload an image.

HttpResponse response = null;
try {
    HttpPost httppost = new HttpPost("http://10.0.2.2:8000/mobile");
    //  HttpPost httppost = new HttpPost("some url");

    MultipartEntity multipartEntity = new MultipartEntity(); //MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
    multipartEntity.addPart("name", new StringBody("nameText"));
    multipartEntity.addPart("place", new StringBody("placeText"));
    multipartEntity.addPart("tag", new StringBody("tagText"));
    //multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT));
    multipartEntity.addPart("Image", new FileBody(destination));
    httppost.setEntity(multipartEntity);

    httpclient.execute(httppost, new PhotoUploadResponseHandler());

  } catch (Exception e) {
    Log.e( "Error","error");
  } 

Error message:

Could not find class 'org.apache.http.entity.mime.MultipartEntity'

And I have tried manually creating libs folder and manually including jar files into /libs folder. When I do that It fails to compile.

Error:

Conversion to Dalvik format failed with error 1  Unknown Android Packaging Problem

Tried creating fresh application including libraries. And I encountered the same error. I've tried everything possible. Can anyone tell me why this happens and how to fix it. Any help would be greatly appreciated!!

Answer

Akshay picture Akshay · Nov 2, 2012

If you are using new android-sdk Try this.

  1. Create folder named as libs
  2. Paste your jar file in that folder.
  3. And finally right click on your project > Build Path > Add External Archive.

That's it.This might help.Good Luck.