Solution to the problem of Chinese garbled code when Android programming sends a request to the server

This paper describes the solution to the problem of Chinese garbled code when Android programming sends a request to the server. Share with you for your reference, as follows:

In the andorid project, we send a request to the server through get. The URL parameter has Chinese, which will cause garbled code. There are two reasons for garbled Code:

1. When submitting parameters, the Chinese parameters were not URL encoded

2. The Tomcat server uses ios8859-1 encoding by default (Chinese is not supported) to obtain parameter values

solve:

1. Enter the Android project. When submitting parameters, code the parameter values: @ h_ 404_ 11@

Create a new filter and set all paths to be filtered. Then the URL pattern is: / *. The dofilter method will be called every time the request arrives. The specific code is as follows:

Where encodenrequestwrapper Code:

In this way, Chinese parameters can be handled correctly for all get requests. The above filters adopt the design of decoration mode. About the decoration mode, baidu is as follows:

(1) Decorative objects and real objects have the same interface. In this way, the client object can interact with the decoration object in the same way as the real object@ H_ 404_ 11 @ (2) decoration object contains an index of real object (Reference) @ h_ 404_ 11 @ (3) the decoration object accepts all requests from the client. It forwards these requests to real objects@ H_ 404_ 11 @ (4) the decoration object can add some additional functions before or after forwarding these requests. This ensures that additional functions can be added externally without modifying the structure of a given object at run time. In object-oriented design, the function extension of a given class is usually realized through inheritance.

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>