Android HttpPost data Chinese garbled Solution

Source: Internet
Author: User

When the project submits data to the server in HttpPost mode, Chinese characters on the server display garbled characters. How can this problem be solved? In fact, this problem should be taken into account in the architecture. If it involves different platform applications, we should try to use UTF8 encoding as much as possible, which can be less troublesome. Of course, you can specify the encoding when you post data. refer to the following functions:

·

[Java] public Map <String, Object> CreateNote (int albumId, String title,
String remark ){
String noteId = "";
Map <String, Object> map = new HashMap <String, Object> ();
Try {
HttpParams parms = new BasicHttpParams ();
Parms. setParameter ("charset", HTTP. UTF_8 );
HttpConnectionParams. setConnectionTimeout (parms, 8*1000 );
HttpConnectionParams. setSoTimeout (parms, 8*1000 );
HttpClient httpclient = new DefaultHttpClient (parms );
HttpPost httppost = new HttpPost (ConfigHelper. CreateUri );
Httppost. addHeader ("Authorization", mToken );
Httppost. addHeader ("Content-Type", "application/json ");
Httppost. addHeader ("charset", HTTP. UTF_8 );
 
JSONObject obj = new JSONObject ();
Obj. put ("title", title );
Obj. put ("categoryId", mCategoryId );
Obj. put ("sourceUrl", GetSourceUri ());
 
JSONArray arr = new JSONArray ();
 
Arr. put (DateFormat. format ("yyyyMM", Calendar. getInstance (Locale. CHINA )));
Obj. put ("tags", arr );
Obj. put ("content", remark );
Httppost. setEntity (new StringEntity (obj. toString (), HTTP. UTF_8 ));
HttpResponse response;
Response = httpclient.exe cute (httppost );
Int code = response. getStatusLine (). getStatusCode ();
If (code = ConstanDefine. ErrorCode. SuccOfHttpStatusCode ){
String rev = EntityUtils. toString (response. getEntity ());
Obj = new JSONObject (rev );
NoteId = obj. getString ("id ");
Map. put ("return_code", "0 ");
Map. put ("content", rev );
}
} Catch (Exception e ){
If (map. containsKey ("return_code ")){
Map. remove ("return_code ");
}
Map. put ("return_code", "1 ");
}
Return map;
}


From the pure soul

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.