【第七篇】Volley之处理Gzip数据

 一般对于API请求需带上GZip压缩,因为API返回数据大都是Json串之类字符串,GZip压缩后内容大小大幅降低.

public class GZipRequest extends StringRequest {
    public GZipRequest(int paramInt, String paramString,
            Response.Listener<String> paramListener,
            Response.ErrorListener paramErrorListener) {
        super(paramInt, paramString, paramListener, paramErrorListener);
    }

    public GZipRequest(String paramString,
            Response.Listener<String> paramListener,
            Response.ErrorListener paramErrorListener) {
        super(paramString, paramListener, paramErrorListener);
    }

    protected Response<String> parseNetworkResponse(
            NetworkResponse paramNetworkResponse) {
        String str1 = "";

        try {
            GZIPInputStream gzipInputStream = new GZIPInputStream(
                    new ByteArrayInputStream(paramNetworkResponse.data));
            InputStreamReader inputStreamReader = new InputStreamReader(
                    gzipInputStream);
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            while (true) {
                String str2 = bufferedReader.readLine();
                if (str2 == null)
                    break;
                str1 = str1 + str2;
            }
            inputStreamReader.close();
            bufferedReader.close();
            gzipInputStream.close();
            return Response.success(str1,
                    HttpHeaderParser.parseCacheHeaders(paramNetworkResponse),
                    false);
        } catch (IOException e) {
            return Response.error(new ParseError(e));
        }
    }
}

 

上一篇:SRS源码—— Thread笔记


下一篇:浅谈云计算技术原理和体系结构