Issues with Android Access server (TOMCAT) garbled (reproduced)

Source: Internet
Author: User

Problems caused by Android Access server (TOMCAT) garbled

1, the browser to the server to send the request can be divided into 2 kinds: get, post;delete, head, etc. do not repeat.

Get mode:

The most important feature of knocking the address directly from the browser is that the parameter is directly behind the address.

Post mode: Form submission, and so on.

2. Access process:

In the browser, enter the address –> the browser to encode the Chinese –> sent to the server-server for decoding

How the browser encoding and the server decoding character set inconsistent will cause garbled problems.

3, garbled to solve

The default browser uses UTF-8 encoding (ie default GBK can certainly be set via META tag)

The server (TOMCAT) uses iso-8859-1 decoding by default. Iso-8859-1 is not supported in Chinese, that is, not to do processing, Chinese is a certain garbled.

Post Mode resolution:

For example, form submission, set request.setcharacterencoding ("UTF-8") in a servlet or filter, and it will be a good solution.

Get mode: Simply set request.setcharacterencoding ("UTF-8") is useless, so we change the default iso-8859-1 encoding to UTF-8, in the server.xml of the Tomcat configuration file:

<connector port= "8080" protocol= "http/1.1"

connectiontimeout= "20000"

Redirectport= "8443" uriencoding= "UTF-8"/>

Add Uriencoding=utf-8

4. Access in Android:

Post way, through the HttpURLConnection encapsulation Post request "concrete implementation of Baidu", the Post method parameter is to be packaged sent, not directly following the URL. For example: http://www.demo.com?p=123 This is not a post transfer parameter.

Get way, through the httpurlconnection can be very simple implementation, but will find that it is easy to garbled problems.

First: Add Uriencoding=utf-8 work completed, or will be sent garbled.

This is because the above procedure mentions that the browser encodes Chinese , here we send the request directly, and we do not encode the process, so we need to manually code it ourselves, namely:

String name =urlencoder.encode ("Long Live China", "UTF-8");

So we simulated the browser encoding, and then sent to the server, the server decoding also changed to UTF-8, and then can be happy to play.

Original link This article by Bean John Blog Backup expert remote One click release

Issues with Android Access server (TOMCAT) garbled (reproduced)

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.