android下使用gzip压缩提升访问server api的效率
2016-02-28 22:26
429 查看
使用gzip的流程如下:
1在httprequest请求头中,添加要求支持gzip的key-value,key是accept-encoding
value是 gzip如下所示
httprequest.addHeader("accept-encoding","gzip");
2在服务端的逻辑是,检查http请求头中的accept-encoding是否有gzip值,如果有就执行gzip压缩。
如果执行了gzip压缩,那么返回的httpresponse的头中就有一个content-encoding字段,会带有gzip压缩的值。
3app检查httpresponse头中的content-encoding字段是否包含gzip值,这个值有无,导致了app解析httpresponse的姿势不同
1在httprequest请求头中,添加要求支持gzip的key-value,key是accept-encoding
value是 gzip如下所示
httprequest.addHeader("accept-encoding","gzip");
2在服务端的逻辑是,检查http请求头中的accept-encoding是否有gzip值,如果有就执行gzip压缩。
如果执行了gzip压缩,那么返回的httpresponse的头中就有一个content-encoding字段,会带有gzip压缩的值。
3app检查httpresponse头中的content-encoding字段是否包含gzip值,这个值有无,导致了app解析httpresponse的姿势不同
//根据是否支持gzip来使用不同的解析方式 String strResponse = ""; if ((response.getEntity().getContentEncoding() != null) && (response.getEntity().getContentEncoding().getValue() != null)) { <span style="white-space:pre"> </span>if (response.getEntity().getContentEncoding().getValue().contains("gzip")) { <span style="white-space:pre"> </span>final InputStream in = response.getEntity().getContent();
<span style="white-space:pre"> </span>final InputStream is = new GZIPInputStream(in); <span style="white-space:pre"> </span>strResponse = HttpRequest.inputStreamToString(is); <span style="white-space:pre"> </span>is.close(); <span style="white-space:pre"> </span>} else { response.getEntity().writeTo(content); strResponse = new String(content.toByteArray()).trim(); } } else { <span style="white-space:pre"> </span>response.getEntity().writeTo(content); strResponse = new String(content.toByteArray()).trim(); }
相关文章推荐
- 史上最强Android 开启照相或者是从本地相册选中一张图片以后先裁剪在保存并显示的讲解附源码i
- 区别getFragmentManager、getSupportFragmentManager和getChildFragmentManager
- Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作
- Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作
- android学习之CheckBox
- 【Android学习入门】Android中activity的启动模式
- Android设备的内置存储和外置存储到底是怎么回事,深入理解
- android studio 程序真机运行中文显示乱码
- 超级简单的Android Studio jni 实现(无需命令行)
- Android学习资料
- Android笔记---Handler机制
- Android绘图
- 关于(About)程序信息(Menu功能菜单程序设计)
- Android 用Animation-list实现逐帧动画
- Android -- BroadCastReceiver的简单使用
- android 使用post 提交
- Android中Rect和RectF的区别和使用
- 4.5.5 android AsyncTask介绍
- android学习之ToggleButton实现开关效果
- Android远程服务——AIDL