Volley 请求/接收内容 GZIP压缩

作者: 考特林 | 来源:发表于2018-12-18 17:13 被阅读4次

往后台传输json字符串为例,其他类型同样做法

自定义Request继承Volley自带的StringRequest

GzipRequest.java

public class GzipRequest extends StringRequest {

    private String mData = null;

    public GzipRequest(String url, Response.Listener<String> listener, @Nullable Response.ErrorListener errorListener) {
        super(url, listener, errorListener);
    }

    public GzipRequest(String url,String data, Response.Listener<String> listener, @Nullable Response.ErrorListener errorListener) {
        super(Method.POST,url, listener, errorListener);
        mData = data;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String,String> headers = new HashMap<String, String>(); 
        headers.put("Charset", "UTF-8");
        headers.put("Accept-Encoding", "gzip,deflate");//客户端允许接收GZIP
        headers.put("Content-Encoding", "gzip");//客户端发送GZIP内容
        headers.put("Content-Type", "application/json");//根据后台要求,觉得Content-Type类型
        return headers;
    }

    @Override
    public RetryPolicy getRetryPolicy() {
        return new DefaultRetryPolicy();
        //setRetryPolicy(RetryPolicy policy);可自定义超时时间,重试次数等参数
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        return mData == null ? super.getBody() : compress(mData);
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String result = uncompress(response.data);
        return Response.success(result, HttpHeaderParser.parseCacheHeaders(response));
        //return super.parseNetworkResponse(response);
    }

    @Override
    protected void deliverResponse(String response) {

    }

    private byte[] compress(String str) {
        try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
            try (GZIPOutputStream gzip = new GZIPOutputStream(outStream)) {
                gzip.write(str.getBytes(StandardCharsets.UTF_8));
            }
            return out.toByteArray();
        }catch (Exception E){
            return new byte[0];
        }
    }

    private String uncompress(byte[] str){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str))) {
            int b;
            while ((b = gis.read()) != -1) {
                baos.write((byte) b);
            }
        }catch (Exception e){
            return "";
        }
        return new String(baos.toByteArray(), StandardCharsets.UTF_8);
    }

}

以上,有帮助到你的话随手点个赞^^

相关文章

  • Volley 请求/接收内容 GZIP压缩

    往后台传输json字符串为例,其他类型同样做法 自定义Request继承Volley自带的StringReques...

  • 前端性能优化套路总结

    一、服务端开启gzip压缩 GZip压缩的基本流程 Web服务器接收到浏览器的HTTP请求后,检查浏览器是否支持H...

  • nginx常用配置

    开启nginx的gzip压缩,通常css或js文件比较大时用到,能有效的提高页面加载速度 设置nginx接收请求最...

  • nginx优化

    Nginx gzip压缩模块提供了压缩文件内容的功能,用户请求的内容在发送到用户客户端之前,Nginx服务器会根据...

  • 关于前端性能优化的一些总结

    性能优化 1、减少HTTP请求 2、使用内容发布网络 3、添加Expires头 4、压缩组件(gZip) 5、将样...

  • C# SharpZipLib解压缩/压缩Gzip数据

    Gzip压缩: Gzip解压缩:

  • linux/unix之压缩文件

    gzip压缩数据 单个压缩 gzip test.log 批量压缩 gzip test* 归档数据 tar命令 压缩...

  • 每天学点Linux命令(四)

    更多内容请关注我的个人博客: 每天学点Linux命令(四) gzip 命令 gzip命令只能压缩单个文件,无法压缩...

  • nginx开启gzip

    1.开启gzip nginx实现资源压缩的原理是通过ngx_http_gzip_module模块拦截请求,并对需要...

  • gzip和tar使用指南

    gzip简介: 用于压缩和解压的工具 常用命令: 查看gz压缩包中的内容:gzip -l archive.gz 或...

网友评论

    本文标题:Volley 请求/接收内容 GZIP压缩

    本文链接:https://www.haomeiwen.com/subject/xqmlkqtx.html