Go to Android HttpClient post multipartentity-android upload file

Source: Internet
Author: User

Transfer from http://blog.csdn.net/hellohaifei/article/details/9707089

Using Httpclient,multipartentity in Android

In order to send pictures, files and other resources, now using open source org.apache.http.entity.mime.MultipartEntity

One. Go to official website http://hc.apache.org/downloads.cgi download

You can download binary only, and if you may need to modify the source file, you can download it directly.

Two. Importing the jar Package

Take the downloaded Httpcomponents-client-4.2.5-bin.zip to its httpcomponents-client-4.2.5-bin.zip\httpcomponents-client-4.2.5\lib \httpmime-4.2.5.jar Bag

Place the Httpmime-4.2.5.jar package in the Lib directory of the Android project.

Three. View the jar package,

I'm using the source file here, because I need to change something.

Three. Use

[Java]View Plaincopy
  1. Class Myasynctask extends Asynctask<string, Integer, string> {
  2. String form_table_name = "ask?action=chatbottom-tosay-"; The form you need to configure
  3. String FilePath = "/mnt/sdcard/picture.jpg"; Test the file path of the write, convert it to its own file path
  4. final String Hosturl = "http://www.myhost.com";//write the address you want to upload
  5. @Override
  6. protected string Doinbackground (String ... params) {
  7. HttpClient HttpClient = null;
  8. HttpClient = new Defaulthttpclient ();
  9. final HttpPost httppost = new HttpPost (Hosturl);
  10. Final File ImageFile = new File (FilePath);
  11. final Multipartentity multipartentity = new Multipartentity ();
  12. if (false) {
  13. InputStream in = null;
  14. try {
  15. in = new FileInputStream (ImageFile);
  16. } catch (FileNotFoundException e) {
  17. E.printstacktrace ();
  18. }
  19. Inputstreambody inputstreambody = new Inputstreambody (in,
  20. "Android_inputstream.jpg");
  21. //Formbodypart Formbodypart = new Formbodypart (Form_table_name,
  22. //Contentbody);
  23. Multipartentity.addpart (Form_table_name, inputstreambody);
  24. }
  25. if (false) {
  26. Contentbody contentbody = new Filebody (ImageFile);
  27. Formbodypart Formbodypart = new Formbodypart (Form_table_name,
  28. Contentbody);
  29. Multipartentity.addpart (Formbodypart);
  30. }
  31. if (false) {
  32. //Filebody filebody = new Filebody (ImageFile, "Image/jpeg",
  33. //"Utf-8");
  34. Filebody filebody = new Filebody (ImageFile);
  35. Multipartentity.addpart (Form_table_name, filebody);
  36. }
  37. if (true) {
  38. Bitmap PHOTOBM = Bitmapfactory.decodefile (FilePath);
  39. if (PHOTOBM = = null) {
  40. return null;
  41. }
  42. Bytearrayoutputstream Photobao = new Bytearrayoutputstream ();
  43. Boolean successcompress = Photobm.compress (Compressformat.jpeg,
  44. Photobao);
  45. if (!successcompress) {
  46. return null;
  47. }
  48. Bytearraybody bytearraybody = new Bytearraybody (
  49. Photobao.tobytearray (), "android.jpg");
  50. Photobm.recycle ();
  51. //Inputstreambody inbody = new Inputstreambody (new InputStream,
  52. //filename);
  53. Multipartentity.addpart (Form_table_name, bytearraybody);
  54. }
  55. Httppost.setentity (multipartentity);
  56. HttpResponse HttpResponse;
  57. try {
  58. HttpResponse = Httpclient.execute (HttpPost);
  59. final int statusCode = Httpresponse.getstatusline ()
  60. . Getstatuscode ();
  61. String response = entityutils.tostring (
  62. Httpresponse.getentity (), HTTP. UTF_8);
  63. IWLOG.D ("got response:\n" + response);
  64. if (StatusCode = = HTTPSTATUS.SC_OK) {
  65. return "Success";
  66. }
  67. } catch (Clientprotocolexception e) {
  68. E.printstacktrace ();
  69. } catch (IOException e) {
  70. E.printstacktrace ();
  71. } finally {
  72. if (httpclient! = null) {
  73. Httpclient.getconnectionmanager (). Shutdown ();
  74. HttpClient = null;
  75. }
  76. }
  77. return null;
  78. }
  79. @Override
  80. protected void OnPostExecute (String result) {
  81. Super.onpostexecute (Result);
  82. if (Result.equals ("Success")) {
  83. }
  84. }
  85. }


Four. Comparison with HttpURLConnection

Many people on the Internet are using httpurlconnection to upload pictures, files. As I httpurlconnection in solving practical problems, I was never able to reach my expectations and always die in Urlconnection.getinputstream (). So it is not possible to switch to the above library. Eventually it feels multipartentity easier to use.

Report:

In solving the practical problems, I am not smooth sailing, but also encountered a variety of abstract problems. We recommend a tool Wireshark tool for crawling network protocols. Very helpful

Go to Android HttpClient post multipartentity-android upload file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.