Android Volley frame JSON Chinese garbled problem solving

Source: Internet
Author: User

Recently in the project using volley as the network communication framework, but found that in the transmission of Chinese language will appear garbled. Start by explaining the infrastructure of the project's Hardware and software
On-line:
Django+restful_framework+monogdb
APP:
Android + Volley

Before using the Android test, I used the postman plugin in Chrome to test and found that Chinese reading is normal. Indicates that the server returned a UTF-8 character encoded data.
But why on the Android side will appear garbled phenomenon.
I wonder if there is a problem with the character encoding on the local side?
I just use the transcoding function of the string class to find that it doesn't work.
Bewildered.
I went to search the Internet, mostly volley. The default is to use the UTF8 character encoding format. But the server returned to the UTF-8 string why is the display garbled it.
Then I thought of the source code to view the volley.
I found that the structure of the entire framework of the volley is this way, first of all the Android side constructs different types of request objects, there are always these major categories:

  • Jsonobjectrequest
  • Jsonarrayrequest
  • Stringrequest
    They all have a common base class of--reuqest. All subclasses inheriting the request must overwrite the following two methods:
  • protected Response<T> parseNetworkResponse(NetworkResponse response);
  • protected void deliverResponse(T response);
    The first method is used to parse the raw data returned by the server. The response object contains the body, headers, and so on of the returned data and needs to parse the returned data in the method. For example, Jsonobjectrequest is using the body string in response.
    Constructs an Jsonobject object that is passed to the listener. Such a design defaults to the sender of the message will know how the server's return is resolved by this unspoken rule.
    I then looked at the Parsenetworkresponse method in the Jsonobjectreuqest class to see what it did before passing the result to the listener.
    The source code is as follows:
    @Overrideprotected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {    try {        String jsonString =            new String(response.data, HttpHeaderParser.parseCharset(response.headers));        return Response.success(new JSONObject(jsonString),                HttpHeaderParser.parseCacheHeaders(response));    } catch (UnsupportedEncodingException e) {        return Response.error(new ParseError(e));    } catch (JSONException je) {        return Response.error(new ParseError(je));    }}
    We see that the Jsonobjectrequest class is transcoding strings before returning the data to the listener. We seem to be close to the nature of the problem. Then look at Httpheaderparser.parsecacheheaders (response), how to get the character set. I guess it definitely contains the default character set definition.
    Open code:
    /** * Returns the charset specified in the Content-Type of this header, * or the HTTP default (ISO-8859-1) if none can be found. */public static String parseCharset(Map<String, String> headers) {    String contentType = headers.get(HTTP.CONTENT_TYPE);    if (contentType != null) {        String[] params = contentType.split(";");        for (int i = 1; i < params.length; i++) {            String[] pair = params[i].trim().split("=");            if (pair.length == 2) {                if (pair[0].equals("charset")) {                    return pair[1];                }            }        }    }    return HTTP.DEFAULT_CONTENT_CHARSET;}
    See the note, everything is in the bottom of the original, if you do not specify a character set in the header of the server's return data, the iso-8859-1 character set is used by default.
    The alias of Iso-8859-1 is called Latin1. This character set is supported partly for European languages and does not support Chinese ~
    It is not understandable why this character set is used as the default character set. Volley this framework is used in the context of network communication.
    Spit Groove also no use, we look at how to solve the problem of Chinese garbled. There are several ways to resolve this:
  • In the header of the returned data of the server contenttype plus the Charset=utf-8 declaration.
  • When you are unable to modify the server program, you can define a new subclass. Overwrite Parsenetworkresponse This method, directly use UTF-8 to transcode the return data of the server.
    All right, it's done.
    We summarize that from the solution of this problem, we can obtain the following experience:
    1. Information on the Internet can not be fully trusted, to believe in their own judgments.
    2. Look at the source code is very important, generally through the source code to find a solution to the problem is the fastest. More efficient than aimless search on the Internet.

Android Volley frame JSON Chinese garbled problem solving

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.