Android about HttpClient upload Chinese garbled solution

Source: Internet
Author: User

People who have used HttpClient know that you can add text information to upload by Addtextbody method, but if you want to upload Chinese, or if you have a Chinese name file, the problem is garbled, the solution is simple:

The first step: Set the Multipartentitybuilder encoding method to UTF-8.

Builder.setcharset (Charset.forname (HTTP. Utf_8)); //

Step Two: Create the ContentType object and specify the UTF-8 encoding.

Step three: Use addpart+ stringbody instead of Addtextbody. Such as:

Stringbody stringbody=New stringbody (" Chinese garbled ", ContentType); Builder.addpart ("test", Stringbody);

Enclose the complete code:

ContentType ContentType =contenttype.create (HTTP. Plain_text_type, HTTP.      UTF_8); HttpClient Client=NewDefaulthttpclient ();//turn on a client HTTP requestHttpPost post =NewHttpPost (URL);//Create an HTTP POST requestMultipartentitybuilder Builder =multipartentitybuilder.create (); Builder.setcharset (Charset.forname (HTTP. Utf_8));//set the encoding format of the requestBuilder.setmode (httpmultipartmode.browser_compatible);//set browser compatibility modeintCount=0; for(File file:files) {//filebody filebody = new Filebody (file);//convert files to stream objects Filebody//builder.addpart ("File" +count, filebody);Builder.addbinarybody ("file"+count, file); Count++;} Builder.addtextbody ("Method",params.Get("Method"));//Set Request ParametersBuilder.addtextbody ("FileTypes",params.Get("FileTypes"));//Set Request ParametersStringbody stringbody=NewStringbody ("Chinese garbled", ContentType); Builder.addpart ("Test", Stringbody); Httpentity Entity= Builder.build ();//generate an HTTP POST entityPost.setentity (entity);//Set Request ParametersHttpResponse response = Client.execute (POST);//initiating the request and returning the requested responseif(Response.getstatusline (). Getstatuscode () = = $) {    return true;}return false;

Android about HttpClient upload Chinese garbled solution

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.