Android Projects

Android Development Books

Tuesday

Using MultipartEntity in Android applications

                                                                                                                      - By Fredrik
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.
Other useful resources:
http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/
http://www.androidjavadoc.com/0.9_beta/com/android/internal/http/multipart/MultipartEntity.html
http://stackoverflow.com/questions/5474916/multipartentity-not-creating-good-request
http://www.java2s.com/Open-Source/Android/android-core/platform-frameworks-base/com/android/internal/http/multipart/MultipartEntity.java.htm 
http://groups.google.com/group/android-developers/tree/browse_frm/thread/7805ec350f467ee5/b738808dd66415e8?hide_quotes=no&pli=1

Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0