Android programming to send requests to the server when the Problem of Chinese garbled code solution

  • 2020-11-03 22:35:44
  • OfStack

An example of Android programming to send a request to the server when the Problem of Chinese garbled code solution. To share for your reference, the details are as follows:

In the andorid project, we sent a request to the server through get, in which the url parameter with Chinese characters would produce a scrambled code, and the scrambled code would be generated for two reasons:

1. There is no URL encoding for the Chinese parameters when submitting the parameters

2. Tomcat server adopts es13EN8859-1 code by default (Chinese is not supported) to get parameter values

Solution:

1. Enter the android project, and encode the value of parameters when submitting them:

URLEncoder.encode(value,  encoding ); //"UTF-8"

2. Get the base 2 data of the string through IOS8859, and then get the new string through UTF-8.
String title = new String(value.getBytes("ISO8859-1","UTF-8");

If all requests for the entire web project had to be addressed in the same way as above, it would be cumbersome to do so by using a filter.

Create a new filter and set it to filter all paths, then URL Pattern is: /*, where the doFilter method will be called every time the request comes, 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); 
  } 
}

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"); 
   } catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
   } 
  } 
  return value; 
 } 
}

This will handle the Chinese parameters correctly for all GET requests. The above filter adopts the design of decoration mode. About the decoration mode, Baidu is as follows:

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

I hope this article has been helpful in Android programming.


Related articles: