Next, let me explain how to use gzip for data transmission in the Android system.
GZIP encoding on HTTP is a technology used to improve the performance of WEB applications. Large-Traffic WEB sites often use GZIP compression technology to reduce the file size and reduce the file size. One is to reduce the storage space, and the other is to transfer files over the network, this reduces the transmission time. After writing this blog, the author tested that MB of text data was transferred to the client through Gzip and then changed to kb, which results in extremely high compression efficiency.
I. Server
The server can be compressed in two ways, one can be compressed by itself, but the second method is recommended, with PrintWriter as the output stream. The tool-type code is as follows:Copy codeThe Code is as follows :/**
* Determine whether 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) {// the client does not support gzip.
Return false;
} Else {// supports gzip Compression
Return true;
}
}
/**
* Create a PrintWriter object output in gzip format. If the browser does not support gzip format, create a normal PrintWriter object,
* @ Param req
* @ Param resp
* @ Return
* @ Throws IOException
*/
Public static PrintWriter createGzipPw (HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter pw = null;
If (isGzipSupport (req) {// supports gzip Compression
Pw = new PrintWriter (new GZIPOutputStream (resp. getOutputStream ()));
// Set the return type to gzip in the header
Resp. setHeader ("content-encoding", "gzip ");
} Else {// the client does not support gzip
Pw = resp. getWriter ();
}
Return pw;
}
The servlet code is as follows:Copy codeThe Code is 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 \ ": \" embedded Page \ "},\" VideoComp \ ":{\" title \": \ "video \"},"
+ "\" PopButton \ ": {\" title \ ": \" Content Switch \ "},\" ZoomingPic \ ":{\" title \": \ "zoom in or out a large image \"},"
+ "\" Rotate360 \ ": {\" title \ ": \" 360 degree 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 tracked in the agent software is as follows:Copy codeThe Code is as follows: