Android uses httpclient to transfer files to the server

Source: Internet
Author: User

HttpClient is a sub-project under Apache Jakarta Common that can be used to provide a feature-rich client-side programming toolkit that supports the HTTP protocol, which has a problem writing clients these days, " The client needs to send a POST request to the server at the same time with the request parameters and files ", just contact httpclient, not quite understand, tried for a long while finally found a method, so write an essay to remember.

Problem:

The client sends a POST request to the server with both parameters and files

Development environment:

Android Studio1.22

HttpClient version: Httpclient-android 4.3.5.1

1. Add Dependencies in Build.gradle:

1 Dependencies {2     compile group: ' Org.apache.httpcomponents ', Name: ' Httpclient-android ', Version: ' 4.3.5.1 '3     compile ' org.apache.httpcomponents:httpmime:4.3.6 '4 }

Note: After adding a dependency package, the compilation will report a redefinition error, I downloaded the jar package and looked at it as if this project was built by Maven, HttpClient and Httpmime inside Pom.xml dependencies have duplicates, and Meta-inf folder There are many duplicate files (specific or not very clear, these are only their own understanding, do not know whether it is correct, everyone for reference only), add the following code in the Build.gradle can be resolved The

1 android{  2    packagingoptions {  3         exclude ' meta-inf/dependencies ' 4          exclude ' meta-inf/notice ' 5         exclude ' meta-inf/license ' 6         exclude ' meta-inf/ LICENSE.txt ' 7         exclude ' meta-inf/notice.txt ' 8    }  910  }

2, add the appropriate network permissions and file read/write permissions in Androidmanifest.xml

1     <uses-permissionAndroid:name= "Android.permission.INTERNET"/>2     <!--Create and delete file permissions in SDcard -3     <uses-permissionAndroid:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>4     <!--Write Data permissions to SDcard -5     <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>6     <uses-permissionAndroid:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

3. Uploading Files:

1Closeablehttpclient httpClient =Httpclients.createdefault ();2HttpPostHC4 httpPostHC4 =NewHttpPostHC4 (URL);3File File =NewFile (filePath);4         if(File = =NULL){5             Throw NewException (filepath+ "The specified file does not exist");6         }7Httpentity multipartentity =multipartentitybuilder.create ()8. Addpart ("param1",NewStringbody ("Param1str"))9. Addpart ("param2",NewStringbody ("Param2str"))Ten. Addpart ("File",Newfilebody (file)). build (); One httpposthc4.setentity (multipartentity); ACloseablehttpresponse response = Httpclient.execute (httpPostHC4);

In fact, the main use of multipartentity

Android uses httpclient to transfer files to the server

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.