從源碼角度 解決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;
    }

搞定了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章