The solution to the Chinese garbled problem when the Android program sends the request to the server _android

Source: Internet
Author: User
Tags wrapper tomcat server

This article illustrates the solution to the Chinese garbled problem when the Android program sends the request to the server. Share to everyone for your reference, specific as follows:

We are in the Andorid project through get way to send a request to the server, where the URL parameters with Chinese, will produce garbled, garbled causes two kinds:

1. When submitting parameters, there is no URL encoding for Chinese parameters

2, the Tomcat server by default is IOS8859-1 encoding (does not support Chinese) to get the parameter value

Solve:

1, enter the Android project, in which to submit parameters, the value of the parameter to encode:

Copy Code code as follows:
Urlencoder.encode (value, encoding method); "UTF-8"

2, through the IOS8859 to get the binary data string, and then through the UTF-8 to get a new string.
Copy Code code as follows:
string title = new String (Value.getbytes ("Iso8859-1", "UTF-8");

If all requests in the entire Web project have to be addressed in the above way to solve the coding problem, it will be more cumbersome to use filters to solve.

New filter, set to filter all the paths, then the URL pattern is:/*, where each request comes to call the Dofilter method, the specific code is as follows:

public void Dofilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception { 
  HttpServletRequest req = (httpservletrequest) request; 
  if ("Get". Equals (Req.getmethod ()) { 
   Encodingrequestwrapper wrapper = new Encodingrequestwrapper (req); 
   Chain.dofilter (wrapper, response); 
  } else{ 
   req.setcharacterencoding ("UTF-8"); 
   Chain.dofilter (request, response); 
  } 


Where Encodingrequestwrapper code:

public class Encodingrequestwrapper extends Httpservletrequestwrapper { 
 private httpservletrequest request; 
 Public Encodingrequestwrapper (HttpServletRequest request) { 
  super (request); 
  This.request = Request; 
 } 
 @Override public 
 String getparameter (string name) { 
  String value = Request.getparameter (name); 
  if (value!=null) { 
   try { 
    value = new String (value.getbytes ("iso8859-1"), "UTF-8"); 
   Unsupportedencodingexception e) { 
    e.printstacktrace (); 
   } 
  } 
  return value; 
 } 
}

This will correctly handle the Chinese parameters for all get requests. The above filter uses the decorative pattern design, regarding the adornment pattern, the Baidu is as follows:

(1) Decorative objects and real objects have the same interface. This allows the client object to interact with the decorated object in the same way as the real object.
(2) A decorative object contains an index of a real object (reference)
(3) The Decoration object accepts all requests from the client. It forwards these requests to the real object.
(4) Decorative objects can add additional functionality before or after forwarding these requests. This ensures that, at run time, additional functionality can be added externally without modifying the structure of the given object. In object-oriented design, a function extension to a given class is usually implemented through inheritance.

I hope this article will help you with the Android program.

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.