Solution of android and Server's URLEncodedUtils Garbled Coding Problem

  • 2021-11-02 02:29:43
  • OfStack

In android development, we often encounter the problem of garbled codes. When encountering garbled codes, we must first check whether the coding format at both ends is 1!

1. We submit data using get and post methods, which are available in upload con.setRequestProperty("Charset", "UTF-8"); And httppost.setEntity(new UrlEncodedFormEntity(数据,"UTF-8")); These two methods are used to determine the encoding method. The server side has request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); To determine the encoding format of the response. In this way, there will be no garbled code. If there is garbled code, we can also establish a filter to filter garbled code. I don't talk about filters here, but I will talk about filters separately another day.

What I want to say today is about passing parameters in post, after the path +? + Parameter passing mode, and then accepting garbled problem after passing.

First of all, our Android side should package the data String str=URLEncodedUtils. format (packaged Parameters data, "UTF-8"); Then the default utf-8 encoding, then HttpPath.FABU_PATH + "? "+ str Connects data under the path and submits it as post. Here, URLEncodedUtils is used to encode the data.

Next, we talk about the server acceptance problem. The server will transcode the data by default after receiving it. The default transcoding format of Tomact is ISO_8859-1. All we need to transcode again. Twice transcoding is required, and the server transcoding code is below


String=new String(request.getParameter("runame").getBytes("ISO_8859-1"),"UTF-8") ; 

Then one set of codes for sending data with URLEncodedUtils encoding is as follows

Android: String str=URLEncodedUtils.format( 封装好的Parameters数据,"UTF-8");

java Server: String=new String(request.getParameter("runame").getBytes("ISO_8859-1"),"UTF-8");

runame is a field in the encapsulated data;

Here are a few things about url decoding:

The first way specifies the code UTF-8

Delivery page

URLEncoder.encode(name,"UTF-8")

Get the page

String name=new String(request.getPatameter(name).getBytes("ISO_8859-1"),"UTF-8");

Or pass the page

URLEncoder.encode(name);

Get the page

String name=new String(request.getPatameter(name).getBytes("ISO_8859-1));

The second method does not specify the coding, according to the default coding of the platform.

Cannot be written as a delivery page

URLEncoder.encode(name,"UTF-8")

Get the page

String name=new String(request.getPatameter(name).getBytes("ISO_8859-1));

In this case, there may be garbled code. Because you don't know what the platform code is.

Therefore, we can only use the first or second form. java recommends that we use the first specified code "utf-8"

Today, I tossed for two hours to solve this Chinese garbled problem.

Here is the solution code:

Server side:


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
......( Operation data code is omitted here )
}

Client:


request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

Ignore params, this is a packet sent to the server. Mainly HTTP.UTF_8

The problem will be solved. Don't engage in GBK, this code will have various problems.

Summarize


Related articles: