A solution to the Chinese transmission garbled in Tomcat/android/java interaction

Source: Internet
Author: User

In the recent project, when interacting with the server to transfer information, there is a problem with Chinese garbled. Since the beginning of the code this thing is not very understanding, are smattering state, usually so-called program Transcoding is based on the passing of luck, did not encounter any problems. But this time is not so lucky, Chinese garbled .... , well, nonsense not much to say, straight into the subject 650) this.width=650; "alt=" Smile "src=" http://static.blog.csdn.net/xheditor/xheditor_emot/default/ Smile.gif "/>.


How can I decode it correctly? First of all, it is clear that the source data is what kind of encoding encode over, and then the corresponding encoding method decode to decode correctly, otherwise it will become garbled!


Let's say, for example:

With Tomcat as the server, the default environment code is ios-8859-1, the client encoding mode is UTF-8(usually many people like to set the encoding method to UTF-8 bar, hehe). When the client get/post the data, the value of the parameter contains Chinese, it is important to note that the Get/post is different:

Get, Tomcat will be the default environment encoding iso-8859-1 Way to decode, this will cause the Chinese language to become garbled, to solve this problem, you can reverse code, new string ("garbled string". GetBytes (" Iso-8859-1 ")," UTF-8 "), can be displayed normally.

Post, it is not decoded with Tomcat's default encoding, but is decoded by the encoding in the filter you set (for example, you set UTF-8).


There are, of course, other ways and means, such as you can do two times urlencode.encode("Chinese", "UTF-8")when the Get method is requested, so that the Tomcat server only has to do a U Rldecode.decode ("parameter value", "UTF-8"); You can do it. The reason is: the first urlencode is to convert the Chinese into%, letters and numbers, the second urlencode is to encode%, letters and numbers, so the result of decoding by default using Iso-8859-1 on the Tomcat server is%, letters and numbers, The Urldecode.decode can display Chinese normally.




This article from "Dan Dan Home" blog, reproduced please contact the author!

A solution to the Chinese transmission garbled in Tomcat/android/java interaction

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.