从源码角度 解决Volley框架乱码的问题

用Volley框架,解析json 发现了乱码问题。但是服务器的又不愿
意改,因为他是老板,只能看源码改了。
请参考:http://blog.csdn.net/wanghao200906/article/details/45719995
Volley框架有三个方法
StringRequest;
JsonArrayRequest
JsonObjectRequest
发下他们分别都是继承了JsonRequest 类
然后呢我们又发现 JsonRequest 类 前面有abstract 是抽象的
惯性思想就是 三个集成一个抽象类 那么三个肯定有类似的方法

结果发现了唯一的抽象类是这个
parseNetworkResponse(NetworkResponse response);

那么就在JsonObjectRequest中找到parseNetworkResponse

  @Override
    protected 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));
        }
    }

然后再在点 HttpHeaderParser.parseCharset(response.headers));

    /** * 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;
    }

重点来了。the HTTP default (ISO-8859-1) if none can be found. 如果接受的头信息么有确定 格式,那么就用 ISO-8859-1
有点儿坑爹。

解决乱码方法
1. 把 return HTTP.DEFAULT_CONTENT_CHARSET; 改为return HTTP.UTF_8;
2. 推荐的方法。重写parseNetworkResponse方法。
改为如下代码即可

@Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        String jsonString;

        try {
            if (response.headers.get(HTTP.CONTENT_TYPE) == null) {
// 头如果格式为空那么 -->格式定义为utf-8
                jsonString = new String(response.data, "UTF-8");
            } else {
//如果不为空的话
//一般头的格式 :Content-Type: text/plain; charset=utf-8'
//或者是这样:Content-Type: text/plain
//第二种还得判断
                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));
        }
    }
再改一下parseCharset这个代码,因为如果是第二种 格式也是没有 返回格式的 
public static String MyparseCharset(Map<String, String> headers) {
        String contentType = headers.get(HTTP.CONTENT_TYPE);
        if (contentType != null) {
            String[] params = contentType.split(";");
            if (params.length == 1) {
// 在原基础上加一个判断条件。如果CONTENT_TYPE内容为Content-Type: text/plain 返回URF-8
                return HTTP.UTF_8;
            } else {
                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;
    }

搞定了。

你可能感兴趣的:(源码,乱码,Volley)