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.
how to add four downloaded jar files.., if iam adding to external liberary iam getting error
Conversion to Dalvik format failed with error 1
no solution, but same Problem
See this link: http://bimbim.in/post/2010/09/24/Reason-of-Conversion-to-dalvik-format-failed-with-error-1.aspx, some library you’ve added isn’t dalvik compatible
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
I downloaded all the above mentioned .jar but still “MultipartEntity cannot be resolved to a type”.
Please help.
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!
Thanks. ^_^
Thanks a lot SkyNet for your help…..You are great really!!
very nice post. this is exactly what i was after
If you still have problem,
project -> properties -> Java Build Path -> Order and Export
check httpmime.jar and apache-mime4j.jar
Thanks very much, it works excelent for me, its importan to clean and refresh your proyect before continue
Thanks a lot its work for me….
Pingback: Porting ios to android: http post : Android Community - For Application Development
Pingback: How can i upload an image to .net WebService from Android : Android Community - For Application Development
Thanks a lot SkyNet. It really helps me!