Next, let me explain how to use gzip for data delivery in the Android system.
The gzip encoding on the HTTP protocol is a technique used to improve the performance of Web applications. Large-volume Web sites often use the gzip compression technology to reduce file size, there are two obvious advantages of reducing file size, one is to reduce storage space, the second is the transmission of files over the network, can reduce the transmission time. Writing this blog, the author was tested, 4.4MB of text data after Gzip transfer to the client and then 392KB, compression efficiency is extremely high.
I. Service-side
The server has 2 ways to compress, one can compress itself, but the second way is more recommended, using PrintWriter as the output stream, the tool class code is as follows
Copy Code code as follows:
/**
* Determine if the browser supports gzip compression
* @param req
* @return Boolean value
*/
public static Boolean Isgzipsupport (HttpServletRequest req) {
String headencoding = Req.getheader ("accept-encoding");
if (headencoding = null | | (Headencoding.indexof ("gzip") = = 1)) {//client does not support gzip
return false;
else {//support gzip compression
return true;
}
}
/**
* Create a PrintWriter object that is output in the gzip format, and create an ordinary PrintWriter object if the browser does not support the gzip format.
* @param req
* @param resp
* @return
* @throws IOException
*/
public static PrintWriter CREATEGZIPPW (HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter pw = null;
if (Isgzipsupport (req)) {//Support gzip compression
PW = new PrintWriter (New Gzipoutputstream (Resp.getoutputstream ()));
Set the return type to gzip in header
Resp.setheader ("content-encoding", "gzip");
else {////client does not support gzip
PW = Resp.getwriter ();
}
return PW;
}
The servlet code is as follows:
Copy Code code as follows:
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcharacterencoding ("Utf-8");
Response.setheader ("content-encoding", "gzip");
String ret = "{\ Contentlayer\": {\ "title\": \ "content layer \"},\ "pagelink\": {\ "title\": \ "page jump \"},\ "Webbrowser\": {\ "title\": \ "Browser \"}, "
+ "\ inlinepage\": {\ "title\": \ "inline page \"},\ "videocomp\": {\ "title\": \ "video \"}, "
+ "\" popbutton\ ": {\" title\ ": \" content switch \ "},\" zoomingpic\ ": {\" title\ ": \" shrink enlarge graph \ "},"
+ "\" rotate360\ ": {\" title\ ": \" 360 degrees rotation \ "}}";
PrintWriter pw = new PrintWriter (New Gzipoutputstream (Response.getoutputstream ()));
Pw.write (ret);
Pw.close ();
}
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
This.dopost (request, response);
}
The data that is tracked in the agent software is as follows:
Copy Code code as follows:
‹«vrîï+ií+ñi¬l-r²ªv*é,éiu²r:rëäým ju "ós}2ó² ' e/m>üììë«@òá©ineùåå¨úÿ¬?pàøw¼g^nf^*ètóo™r–™ ' šïœŸ[?¬àÔåc[ Áöç8 – "äç¡»nÿª7@
¢òós3óò2 "' Uœþºýè–ïg÷? Tå-$–¤›+r ¸ðä‡zh¤†ˆ
The actual data are as follows:
Copy Code code as follows:
{"Contentlayer": {"title": "Content Layer"}, "Pagelink": {"title": "Page Jump"}, "WebBrowser": {"title": "Browser"}, "Inlinepage": {"title" : "Inline Page"}, "Videocomp": {"title": "Video"}, "Popbutton": {"title": "Content Switch"}, "Zoomingpic": {"title": "Shrink Magnified"}, "Rotate360": {" Title ": 360 degree Rotation"}}