码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-17 01:12:56      阅读:877      评论:0      收藏:0      [点我收藏+]

标签:

 一般对于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) {
                e.printStackTrace();
            }
             return Response.error(new ParseError());
    }
}

 

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

标签:

原文地址:http://www.cnblogs.com/androidsuperman/p/4815037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!