Android开发中网络请求的压缩 ── GZip的使用

 

Android开发中网络请求的压缩 ── GZip的使用

标签: android网络header服务器exceptionbyte
  8132人阅读  评论(0)  收藏  举报
  分类:
 

「转载请说明出处:http://blog.csdn.net/kepoon/article/details/7482096」

gzip是GNUzip的缩写,它是一个GNU自由软件的文件压缩程序。

HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术。一般服务器中都安装有这个功能模块的,服务器端不需做改动。

当浏览器支持gzip 格式的时候, 服务器端会传输gzip格式的数据。

从Http 技术细节上讲,就是 http request 头中 有 "Accept-Encoding", "gzip" ,response 中就有返回头Content-Encoding=gzip 。

我们现在从浏览器上访问玩啥网站都是gzip格式传输的。

但是我们现在android 客户端,没有用gzip 格式访问。

同样的的道理,我们可以在android 客户端 request 头中加入 "Accept-Encoding", "gzip" ,来让服务器传送gzip 数据。


具体代码如下。

    <span class="kw1" style="font-weight: bold;">private</span> <span class="kw3" style="color: rgb(0, 51, 153);">String</span> getJsonStringFromGZIP<span class="br0" style="color: rgb(0, 153, 0);">(</span>HttpResponse response<span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span>
        <span class="kw3" style="color: rgb(0, 51, 153);">String</span> jsonString <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="kw2" style="color: rgb(0, 0, 102); font-weight: bold;">null</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
        <span class="kw1" style="font-weight: bold;">try</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span>
            <span class="kw3" style="color: rgb(0, 51, 153);">InputStream</span> is <span class="sy0" style="color: rgb(51, 153, 51);">=</span> response.<span class="me1" style="color: rgb(0, 102, 51);">getEntity</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">)</span>.<span class="me1" style="color: rgb(0, 102, 51);">getContent</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="kw3" style="color: rgb(0, 51, 153);">BufferedInputStream</span> bis <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="kw1" style="font-weight: bold;">new</span> <span class="kw3" style="color: rgb(0, 51, 153);">BufferedInputStream</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>is<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            bis.<span class="me1" style="color: rgb(0, 102, 51);">mark</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="nu0" style="color: rgb(204, 102, 204);">2</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="co1" style="color: rgb(102, 102, 102); font-style: italic;">// 取前两个字节</span>
            <span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">byte</span><span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="br0" style="color: rgb(0, 153, 0);">]</span> header <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="kw1" style="font-weight: bold;">new</span> <span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">byte</span><span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="nu0" style="color: rgb(204, 102, 204);">2</span><span class="br0" style="color: rgb(0, 153, 0);">]</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">int</span> result <span class="sy0" style="color: rgb(51, 153, 51);">=</span> bis.<span class="me1" style="color: rgb(0, 102, 51);">read</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>header<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="co1" style="color: rgb(102, 102, 102); font-style: italic;">// reset输入流到开始位置</span>
            bis.<span class="me1" style="color: rgb(0, 102, 51);">reset</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="co1" style="color: rgb(102, 102, 102); font-style: italic;">// 判断是否是GZIP格式</span>
            <span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">int</span> headerData <span class="sy0" style="color: rgb(51, 153, 51);">=</span> getShort<span class="br0" style="color: rgb(0, 153, 0);">(</span>header<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="co1" style="color: rgb(102, 102, 102); font-style: italic;">// Gzip 流 的前两个字节是 0x1f8b</span>
            <span class="kw1" style="font-weight: bold;">if</span> <span class="br0" style="color: rgb(0, 153, 0);">(</span>result <span class="sy0" style="color: rgb(51, 153, 51);">!=</span> <span class="sy0" style="color: rgb(51, 153, 51);">-</span><span class="nu0" style="color: rgb(204, 102, 204);">1</span> <span class="sy0" style="color: rgb(51, 153, 51);">&&</span> headerData <span class="sy0" style="color: rgb(51, 153, 51);">==</span> 0x1f8b<span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span>
                LogUtil.<span class="me1" style="color: rgb(0, 102, 51);">d</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="st0" style="color: rgb(0, 0, 255);">"HttpTask"</span>, <span class="st0" style="color: rgb(0, 0, 255);">" use GZIPInputStream  "</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
                is <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="kw1" style="font-weight: bold;">new</span> <span class="kw3" style="color: rgb(0, 51, 153);">GZIPInputStream</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>bis<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="br0" style="color: rgb(0, 153, 0);">}</span> <span class="kw1" style="font-weight: bold;">else</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span>
                LogUtil.<span class="me1" style="color: rgb(0, 102, 51);">d</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="st0" style="color: rgb(0, 0, 255);">"HttpTask"</span>, <span class="st0" style="color: rgb(0, 0, 255);">" not use GZIPInputStream"</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
                is <span class="sy0" style="color: rgb(51, 153, 51);">=</span> bis<span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="br0" style="color: rgb(0, 153, 0);">}</span>
            <span class="kw3" style="color: rgb(0, 51, 153);">InputStreamReader</span> reader <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="kw1" style="font-weight: bold;">new</span> <span class="kw3" style="color: rgb(0, 51, 153);">InputStreamReader</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>is, <span class="st0" style="color: rgb(0, 0, 255);">"utf-8"</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">char</span><span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="br0" style="color: rgb(0, 153, 0);">]</span> data <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="kw1" style="font-weight: bold;">new</span> <span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">char</span><span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="nu0" style="color: rgb(204, 102, 204);">100</span><span class="br0" style="color: rgb(0, 153, 0);">]</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">int</span> readSize<span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="kw3" style="color: rgb(0, 51, 153);">StringBuffer</span> sb <span class="sy0" style="color: rgb(51, 153, 51);">=</span> <span class="kw1" style="font-weight: bold;">new</span> <span class="kw3" style="color: rgb(0, 51, 153);">StringBuffer</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="kw1" style="font-weight: bold;">while</span> <span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>readSize <span class="sy0" style="color: rgb(51, 153, 51);">=</span> reader.<span class="me1" style="color: rgb(0, 102, 51);">read</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>data<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="nu0" style="color: rgb(204, 102, 204);">0</span><span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span>
                sb.<span class="me1" style="color: rgb(0, 102, 51);">append</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>data, <span class="nu0" style="color: rgb(204, 102, 204);">0</span>, readSize<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            <span class="br0" style="color: rgb(0, 153, 0);">}</span>
            jsonString <span class="sy0" style="color: rgb(51, 153, 51);">=</span> sb.<span class="me1" style="color: rgb(0, 102, 51);">toString</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            bis.<span class="me1" style="color: rgb(0, 102, 51);">close</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
            reader.<span class="me1" style="color: rgb(0, 102, 51);">close</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
        <span class="br0" style="color: rgb(0, 153, 0);">}</span> <span class="kw1" style="font-weight: bold;">catch</span> <span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="kw3" style="color: rgb(0, 51, 153);">Exception</span> e<span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span>
            LogUtil.<span class="me1" style="color: rgb(0, 102, 51);">e</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="st0" style="color: rgb(0, 0, 255);">"HttpTask"</span>, e.<span class="me1" style="color: rgb(0, 102, 51);">toString</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">)</span>,e<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
        <span class="br0" style="color: rgb(0, 153, 0);">}</span>
 
        LogUtil.<span class="me1" style="color: rgb(0, 102, 51);">d</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="st0" style="color: rgb(0, 0, 255);">"HttpTask"</span>, <span class="st0" style="color: rgb(0, 0, 255);">"getJsonStringFromGZIP net output : "</span> <span class="sy0" style="color: rgb(51, 153, 51);">+</span> jsonString <span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
        <span class="kw1" style="font-weight: bold;">return</span> jsonString<span class="sy0" style="color: rgb(51, 153, 51);">;</span>
    <span class="br0" style="color: rgb(0, 153, 0);">}</span>
 
    <span class="kw1" style="font-weight: bold;">private</span> <span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">int</span> getShort<span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">byte</span><span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="br0" style="color: rgb(0, 153, 0);">]</span> data<span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="br0" style="color: rgb(0, 153, 0);">{</span>
        <span class="kw1" style="font-weight: bold;">return</span> <span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="kw4" style="color: rgb(0, 0, 102); font-weight: bold;">int</span><span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="br0" style="color: rgb(0, 153, 0);">(</span><span class="br0" style="color: rgb(0, 153, 0);">(</span>data<span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="nu0" style="color: rgb(204, 102, 204);">0</span><span class="br0" style="color: rgb(0, 153, 0);">]</span><span class="sy0" style="color: rgb(51, 153, 51);"><<</span><span class="nu0" style="color: rgb(204, 102, 204);">8</span><span class="br0" style="color: rgb(0, 153, 0);">)</span> <span class="sy0" style="color: rgb(51, 153, 51);">|</span> data<span class="br0" style="color: rgb(0, 153, 0);">[</span><span class="nu0" style="color: rgb(204, 102, 204);">1</span><span class="br0" style="color: rgb(0, 153, 0);">]</span><span class="sy0" style="color: rgb(51, 153, 51);">&</span>0xFF<span class="br0" style="color: rgb(0, 153, 0);">)</span><span class="sy0" style="color: rgb(51, 153, 51);">;</span>
    <span class="br0" style="color: rgb(0, 153, 0);">}</span>

参考 ,注意实际使用中,我发现gzip 流前两个字节是0x1e8b ,不是0x1f8b .后来检查一下code ,代码处理错误,加上第二个字节的时候需 &0xFF

0x1f8b 可参考标准 http://www.gzip.org/zlib/rfc-gzip.html#file-format

http://xmagicj.diandian.com/post/2011-08-08/3641381

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值