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