android开发中使用GZIP压缩传输
2013-03-21 10:00
393 查看
<PRE class=java name="code">HTTP 压缩可以大大提高浏览网站的速度,它的原理是,在客户端请求网页后,从服务器端将网页文件压缩,再下载到客户端,由客户端的浏览器负责解压缩并浏览。相对于普通的浏览过程 HTML ,CSS,Javascript , Text ,它可以节省 40% 左右的流量。更为重要的是,它可以对动态生成的,包括 CGI 、 PHP , JSP , ASP , Servlet,SHTML 等输出的网页也能进行压缩,压缩效率惊人
1 < Connector port ="80" maxHttpHeaderSize ="8192"
2 maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75"
3 enableLookups ="false" redirectPort ="8443" acceptCount ="100"
4 connectionTimeout ="20000" disableUploadTimeout ="true" URIEncoding ="utf-8" />
5 <!-- Note : To disable connection timeouts, set connectionTimeout value
6 to 0 -->
7
8 <!-- Note : To use gzip compression you could set the following properties :
9
10 compression="on"
11 compressionMinSize="2048"
12 noCompressionUserAgents="gozilla, traviata"
13 compressableMimeType="text/html,text/xml"
14 -->
从上面的 第 8 行 内容可以看出,要使用 gzip 压缩功能,你可以在 Connector 实例中加上如下属性即可
1) compression="on" 打开压缩功能
2) compressionMinSize="2048" 启用压缩的输出内容大小,这里面默认为 2KB
3) noCompressionUserAgents="gozilla, traviata" 对于以下的浏览器,不启用压缩
4) compressableMimeType="text/html,text/xml" 压缩类型
我这里的配置内容为:
1 < Connector port ="80" maxHttpHeaderSize ="8192"
2 maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75"
3 enableLookups ="false" redirectPort ="8443" acceptCount ="100"
4 connectionTimeout ="20000" disableUploadTimeout ="true" URIEncoding ="utf-8"
5 compression ="on"
6 compressionMinSize ="2048"
7 noCompressionUserAgents ="gozilla, traviata"
8 compressableMimeType ="text/html,text/xml,text/javascript,text/css,text/plain" />
9 <!-- Note : To disable connection timeouts, set connectionTimeout value
10 to 0 -->
11
12 <!-- Note : To use gzip compression you could set the following properties :
13
14 compression="on"
15 compressionMinSize="2048"
16 noCompressionUserAgents="gozilla, traviata"
17 compressableMimeType="text/html,text/xml"
18 -->
19
一旦启用了这个压缩功能后,我们怎么来测试压缩是否有效呢?首先 Tomcat 是根据浏览器请求头中的 accept-encoding来判断浏览器是否支持压缩功能,如果这个值包含有 gzip ,就表明浏览器支持 gzip 压缩内容的浏览,所以我们可以用httpclient 来写一个这样的简单测试程序
HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术。一般服务器中都安装有这个功能模块的,服务器端不需做改动。
当浏览器支持gzip 格式的时候, 服务器端会传输gzip格式的数据。
从Http 技术细节上讲,就是 http request 头中 有 "Accept-Encoding", "gzip" ,response 中就有返回头Content-Encoding=gzip 。
我们现在从浏览器上访问玩啥网站都是gzip格式传输的。
但是我们现在Android 客户端,没有用gzip 格式访问。
同样的的道理,我们可以在android 客户端 request 头中加入 "Accept-Encoding", "gzip" ,来让服务器传送gzip 数据。
具体代码如下。
private String getJsonStringFromGZIP(HttpResponse response) {
String jsonString = null;
try {
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
bis.mark(2);
// 取前两个字节
byte[] header = new byte[2];
int result = bis.read(header);
// reset输入流到开始位置
bis.reset();
// 判断是否是GZIP格式
int headerData = getShort(header);
// Gzip 流 的前两个字节是 0x1f8b
if (result != -1 && headerData == 0x1f8b) { LogUtil.d("HttpTask", " use GZIPInputStream ");
is = new GZIPInputStream(bis);
} else {
LogUtil.d("HttpTask", " not use GZIPInputStream");
is = bis;
}
InputStreamReader reader = new InputStreamReader(is, "utf-8");
char[] data = new char[100];
int readSize;
StringBuffer sb = new StringBuffer();
while ((readSize = reader.read(data)) > 0) {
sb.append(data, 0, readSize);
}
jsonString = sb.toString();
bis.close();
reader.close();
} catch (Exception e) {
LogUtil.e("HttpTask", e.toString(),e);
}
LogUtil.d("HttpTask", "getJsonStringFromGZIP net output : " + jsonString );
return jsonString;
}
private int getShort(byte[] data) {
return (int)((data[0]<<8) | data[1]&0xFF);
}
参考 ,注意实际使用中,我发现gzip 流前两个字节是0x1e8b ,不是0x1f8b .后来检查一下code ,代码处理错误,加上第二个字节的时候需 &0xFF
1 < Connector port ="80" maxHttpHeaderSize ="8192"
2 maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75"
3 enableLookups ="false" redirectPort ="8443" acceptCount ="100"
4 connectionTimeout ="20000" disableUploadTimeout ="true" URIEncoding ="utf-8" />
5 <!-- Note : To disable connection timeouts, set connectionTimeout value
6 to 0 -->
7
8 <!-- Note : To use gzip compression you could set the following properties :
9
10 compression="on"
11 compressionMinSize="2048"
12 noCompressionUserAgents="gozilla, traviata"
13 compressableMimeType="text/html,text/xml"
14 -->
从上面的 第 8 行 内容可以看出,要使用 gzip 压缩功能,你可以在 Connector 实例中加上如下属性即可
1) compression="on" 打开压缩功能
2) compressionMinSize="2048" 启用压缩的输出内容大小,这里面默认为 2KB
3) noCompressionUserAgents="gozilla, traviata" 对于以下的浏览器,不启用压缩
4) compressableMimeType="text/html,text/xml" 压缩类型
我这里的配置内容为:
1 < Connector port ="80" maxHttpHeaderSize ="8192"
2 maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75"
3 enableLookups ="false" redirectPort ="8443" acceptCount ="100"
4 connectionTimeout ="20000" disableUploadTimeout ="true" URIEncoding ="utf-8"
5 compression ="on"
6 compressionMinSize ="2048"
7 noCompressionUserAgents ="gozilla, traviata"
8 compressableMimeType ="text/html,text/xml,text/javascript,text/css,text/plain" />
9 <!-- Note : To disable connection timeouts, set connectionTimeout value
10 to 0 -->
11
12 <!-- Note : To use gzip compression you could set the following properties :
13
14 compression="on"
15 compressionMinSize="2048"
16 noCompressionUserAgents="gozilla, traviata"
17 compressableMimeType="text/html,text/xml"
18 -->
19
一旦启用了这个压缩功能后,我们怎么来测试压缩是否有效呢?首先 Tomcat 是根据浏览器请求头中的 accept-encoding来判断浏览器是否支持压缩功能,如果这个值包含有 gzip ,就表明浏览器支持 gzip 压缩内容的浏览,所以我们可以用httpclient 来写一个这样的简单测试程序
HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术。一般服务器中都安装有这个功能模块的,服务器端不需做改动。
当浏览器支持gzip 格式的时候, 服务器端会传输gzip格式的数据。
从Http 技术细节上讲,就是 http request 头中 有 "Accept-Encoding", "gzip" ,response 中就有返回头Content-Encoding=gzip 。
我们现在从浏览器上访问玩啥网站都是gzip格式传输的。
但是我们现在Android 客户端,没有用gzip 格式访问。
同样的的道理,我们可以在android 客户端 request 头中加入 "Accept-Encoding", "gzip" ,来让服务器传送gzip 数据。
具体代码如下。
private String getJsonStringFromGZIP(HttpResponse response) {
String jsonString = null;
try {
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
bis.mark(2);
// 取前两个字节
byte[] header = new byte[2];
int result = bis.read(header);
// reset输入流到开始位置
bis.reset();
// 判断是否是GZIP格式
int headerData = getShort(header);
// Gzip 流 的前两个字节是 0x1f8b
if (result != -1 && headerData == 0x1f8b) { LogUtil.d("HttpTask", " use GZIPInputStream ");
is = new GZIPInputStream(bis);
} else {
LogUtil.d("HttpTask", " not use GZIPInputStream");
is = bis;
}
InputStreamReader reader = new InputStreamReader(is, "utf-8");
char[] data = new char[100];
int readSize;
StringBuffer sb = new StringBuffer();
while ((readSize = reader.read(data)) > 0) {
sb.append(data, 0, readSize);
}
jsonString = sb.toString();
bis.close();
reader.close();
} catch (Exception e) {
LogUtil.e("HttpTask", e.toString(),e);
}
LogUtil.d("HttpTask", "getJsonStringFromGZIP net output : " + jsonString );
return jsonString;
}
private int getShort(byte[] data) {
return (int)((data[0]<<8) | data[1]&0xFF);
}
参考 ,注意实际使用中,我发现gzip 流前两个字节是0x1e8b ,不是0x1f8b .后来检查一下code ,代码处理错误,加上第二个字节的时候需 &0xFF
相关文章推荐
- android开发中使用GZIP压缩传输
- Android GZip的使用-开发中网络请求的压缩实例详解
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- android接口中json数据的传输中使用gzip压缩
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- Android开发中网络请求的压缩 ── GZip的使用
- 服务器使用Gzip压缩数据,加快网络传输(Java 例子)
- 减小网络传输图片大小,使用gzip进行压缩
- android与服务端通讯时使用到的GZIP压缩及解压
- Android开发--网络请求中GZip的使用(二)
- 服务器使用Gzip压缩数据,加快网络传输(Java 例子)
- 在Unity3d中使用GZip来压缩网络传输数据
- 服务器使用Gzip压缩数据,加快网络传输(Java 例子)
- Android GZIP压缩IO流,优化APP数据传输(一)
- php使用gzip压缩传输js和css文件的方法
- android下使用gzip压缩提升访问server api的效率