Urlencodedutils garbled encoding problem for Android and server

Source: Internet
Author: User

In the development of Android we often encounter with garbled problem, encountered garbled problem first we have to check the two-side encoding format is consistent!

generally we submit data with the Get and post methods, both methods in the upload have Con.setrequestproperty ("Charset", "UTF-8"); and Httppost.setentity (new Urlencodedformentity (data, "UTF-8")); These two ways to determine the encoding method Server side has request.setcharacterencoding ("UTF-8"); response.setcharacterencoding ("UTF-8") to determine the encoding format of the response. This generally does not appear garbled, if there is garbled we can also create a filter to filter garbled, here I do not talk about filters, another day to say a separate filter.

What I want to say today is about passing parameters in post, after the path +? + The parameter is passed and then the accepted garbled question is passed.

First of all, we want to encapsulate the data string Str=urlencodedutils.format (encapsulated parameters data, "UTF-8"); Then the default Utf-8 encoding, and then httppath.fabu_path+ "?" +str the data to the bottom of the path by post. Here we use the urlencodedutils to encode the data.


Next, the server accepts the problem. The server accepts the data after the default transcoding, Tomact's default transcoding format is iso_8859-1 all we want to transcode again. Requires two transcoding, below is the server transcoding code

String=new String (Request.getparameter ("Runame"). GetBytes ("Iso_8859-1"), "UTF-8");

Then a set of code to send data using Urlencodedutils encoding is as follows

Android:String str=urlencodedutils.format (Encapsulated parameters data, "UTF-8");

Java server: String=new String (Request.getparameter ("Runame"). GetBytes ("Iso_8859-1"), "UTF-8");

Runame is a field in the encapsulated data;

Here are some things about URL decoding:

The first way to specify the encoding UTF-8

Delivery page

Urlencoder.encode (name, "UTF-8")

Get page

String Name=new string (request.getpatameter (name). GetBytes ("Iso_8859-1"), "UTF-8");

Or

Delivery page

Urlencoder.encode (name);

Get page

String Name=new string (request.getpatameter (name). GetBytes ("iso_8859-1)");

The second way is to not specify the encoding, by default encoding by platform.

Cannot be written

Delivery page

Urlencoder.encode (name, "UTF-8")

Get page

String Name=new string (request.getpatameter (name). GetBytes ("iso_8859-1)");

In this case, there may be garbled. Because you don't know what the platform code is.

So only in the first or second form, Java recommends that we use the first specified code "UTF-8"


Urlencodedutils garbled encoding problem for Android and server

Related Article

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.