Android中Gzip使用
16lz
2021-01-23
Gzip
http数据传输,body压缩。节省流量
Android开发中网络请求的压缩 ── GZip的使用
http client对post内容gzip压缩和server端解压接收
Android中使用gzip传递数据
以上是参考资料
下面是我测试效果
fiddler 抓包
代码
public static void reqeust(String pUrl) { URL url; try { url = new URL(pUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setRequestProperty("Accept-Encoding", "gzip"); conn.connect(); InputStream is = conn.getInputStream(); int len = 0; byte[] gBuffer = new byte[1024]; StringBuffer sb = new StringBuffer(); while ((len = is.read(gBuffer)) >= 0) { sb.append(new String(gBuffer, 0, len)); } LogUtils.d("TestHttp response " + sb.toString()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
抓包截图
勾选gzip后大小
关键代码
conn.setRequestProperty(“Accept-Encoding”, “gzip”);
测试效果如下为:
- 加了这句话:
效果为:1414 bytes
返回值用上面代码读出来为乱码,需要GzipInputStream读取
gis = new GZIPInputStream(inputStream); int len = 0; byte[] gBuffer = new byte[1024]; StringBuffer sb = new StringBuffer(); while ((len = gis.read(gBuffer)) >= 0) { sb.append(new String(gBuffer, 0, len)); }
2 2. 不加这句话 1414 bytes
抓包显示 Client Accept-Encoding:gzip 默认好像带了。。
然后返回值正常读取,没有乱码,不需要GzipInputStream,
3.conn.setRequestProperty(“Accept-Encoding”, “”);
传值空
效果为: 9665 bytes
返回值也能直接看到。
测试结果!
结论就是啥也不用干。。。。!!!
如我测试方式有问题,谢谢指出
更多相关文章
- Android动画效果 translate、scale、alpha、rotate 切换Activity
- Android自动化测试初探
- GDB在线调试Android Framework Native C/C++代码
- android 发送短信,彩信,邮件代码
- android 图片LOMO效果
- 关机重启代码
- Android 重启应用代码
- 蓝牙原理Android代码实现
- 在文字周围环绕一层阴影效果[android:shadow] 使用