Using MultipartEntity in Android applications

To be able to upload files from an Android application and use the Multipart content type you need to add some additional jar files to your application.

The files needed are apache-mime4j, httpclient, httpcore and httpmime. All are opensource projects built by the Apache foundation.

Download the 4 files and add them to your project then you should be able to use the following code to post strings and files to pages.

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.tumblr.com/api/write");

try {
	MultipartEntity entity = new MultipartEntity();

	entity.addPart("type", new StringBody("photo"));
	entity.addPart("data", new FileBody(image));
	httppost.setEntity(entity);
	HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}

The image variable in this case is a File that contains an image captured by the camera on the phone.

This entry was posted in Code and tagged , , , . Bookmark the permalink.

15 Responses to Using MultipartEntity in Android applications

  1. rahul says:

    how to add four downloaded jar files.., if iam adding to external liberary iam getting error
    Conversion to Dalvik format failed with error 1

  2. Tim says:

    no solution, but same Problem

  3. john says:

    You need to get
    httpcomponents-client-4.1.zip and apache-mime4j-0.6.1-bin.zip

    Add
    apache-mime4j-0.6.1.jar from apache-mime4j-0.6.1-bin.zip
    And
    httpclient-4.1.jar
    httpcore-4.1.jar
    httpmime-4.1.jar
    from the lib folder in httpcomponents-client-4.1.zip

  4. Siva says:

    I downloaded all the above mentioned .jar but still “MultipartEntity cannot be resolved to a type”.
    Please help.

  5. SkyNet says:

    Thanks very much! It works!

    - I downloaded version 4.1.2 from “http://apache.rediris.es//httpcomponents/httpclient/binary/httpcomponents-client-4.1.2-bin.zip”
    and “Apache Mime4J” from “http://apache.rediris.es//james/mime4j/apache-mime4j-0.7-bin.zip”

    - Then open both files and extract mentioned files from them to my “androidproject/lib” folder:
    androidprojecto/lib/…
    pache-mime4j-core-0.7.jar
    httpclient-4.1.2.jar
    httpcore-4.1.2.jar
    httpmime-4.1.2.jar

    - Go to “project properies / Java Build Path / Libraries” and add those 4 libraries.

    - Then clean proyect, refresh, and then my project suggested fixing MultipartEntity IMPORTING from added libraries “org.apache.http.entity.mime.MultipartEntity”!!! IT WORKS!

    thank you!

  6. Lea says:

    Thanks. ^_^

  7. Abid Ali says:

    Thanks a lot SkyNet for your help…..You are great really!!

  8. zeeshan says:

    very nice post. this is exactly what i was after :)

  9. JD Kim says:

    If you still have problem,
    project -> properties -> Java Build Path -> Order and Export
    check httpmime.jar and apache-mime4j.jar

  10. José Torres says:

    Thanks very much, it works excelent for me, its importan to clean and refresh your proyect before continue

  11. prasanth says:

    Thanks a lot its work for me….

  12. Pingback: Porting ios to android: http post : Android Community - For Application Development

  13. Pingback: How can i upload an image to .net WebService from Android : Android Community - For Application Development

  14. Serhii says:

    Thanks a lot SkyNet. It really helps me!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>