Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android post image to the Server using MultipartEntity

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!!

like image 770
Geetanjali Avatar asked Nov 02 '12 05:11

Geetanjali


1 Answers

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.

like image 100
Akshay Avatar answered Nov 02 '22 22:11

Akshay