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
- Class Myasynctask extends Asynctask<string, Integer, string> {
- String form_table_name = "ask?action=chatbottom-tosay-"; The form you need to configure
- String FilePath = "/mnt/sdcard/picture.jpg"; Test the file path of the write, convert it to its own file path
- final String Hosturl = "http://www.myhost.com";//write the address you want to upload
- @Override
- protected string Doinbackground (String ... params) {
- HttpClient HttpClient = null;
- HttpClient = new Defaulthttpclient ();
- final HttpPost httppost = new HttpPost (Hosturl);
- Final File ImageFile = new File (FilePath);
- final Multipartentity multipartentity = new Multipartentity ();
- if (false) {
- InputStream in = null;
- try {
- in = new FileInputStream (ImageFile);
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- }
- Inputstreambody inputstreambody = new Inputstreambody (in,
- "Android_inputstream.jpg");
- //Formbodypart Formbodypart = new Formbodypart (Form_table_name,
- //Contentbody);
- Multipartentity.addpart (Form_table_name, inputstreambody);
- }
- if (false) {
- Contentbody contentbody = new Filebody (ImageFile);
- Formbodypart Formbodypart = new Formbodypart (Form_table_name,
- Contentbody);
- Multipartentity.addpart (Formbodypart);
- }
- if (false) {
- //Filebody filebody = new Filebody (ImageFile, "Image/jpeg",
- //"Utf-8");
- Filebody filebody = new Filebody (ImageFile);
- Multipartentity.addpart (Form_table_name, filebody);
- }
- if (true) {
- Bitmap PHOTOBM = Bitmapfactory.decodefile (FilePath);
- if (PHOTOBM = = null) {
- return null;
- }
- Bytearrayoutputstream Photobao = new Bytearrayoutputstream ();
- Boolean successcompress = Photobm.compress (Compressformat.jpeg,
- Photobao);
- if (!successcompress) {
- return null;
- }
- Bytearraybody bytearraybody = new Bytearraybody (
- Photobao.tobytearray (), "android.jpg");
- Photobm.recycle ();
- //Inputstreambody inbody = new Inputstreambody (new InputStream,
- //filename);
- Multipartentity.addpart (Form_table_name, bytearraybody);
- }
- Httppost.setentity (multipartentity);
- HttpResponse HttpResponse;
- try {
- HttpResponse = Httpclient.execute (HttpPost);
- final int statusCode = Httpresponse.getstatusline ()
- . Getstatuscode ();
- String response = entityutils.tostring (
- Httpresponse.getentity (), HTTP. UTF_8);
- IWLOG.D ("got response:\n" + response);
- if (StatusCode = = HTTPSTATUS.SC_OK) {
- return "Success";
- }
- } catch (Clientprotocolexception e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- } finally {
- if (httpclient! = null) {
- Httpclient.getconnectionmanager (). Shutdown ();
- HttpClient = null;
- }
- }
- return null;
- }
- @Override
- protected void OnPostExecute (String result) {
- Super.onpostexecute (Result);
- if (Result.equals ("Success")) {
- }
- }
- }
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