SoFunction
Updated on 2025-03-10

Use gzip to pass data instance code in Android system

Next, let me explain how to use gzip for data transmission in Android system
GZIP encoding on the HTTP protocol is a technology used to improve the performance of WEB applications. WEB sites with large traffic often use GZIP compression technology to reduce file size. There are two obvious benefits to reducing file size: one is to reduce storage space, and the other is to reduce the transmission time when transferring files over the network. The author tested this blog and after testing it, the 4.4MB text data was transferred to the client through Gzip and then turned to 392KB, which was extremely compressed.

1. Server side
There are two ways to compress the server, one can compress it yourself, but the second method is recommended, using PrintWriter as the output stream, the tool class code is as follows
Copy the 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 = ("accept-encoding");
if (headEncoding == null || (("gzip") == -1)) { // Client does not support gzip
return false;
} else { // Support 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)) { // Support gzip compression
pw = new PrintWriter(new GZIPOutputStream(()));
// Set the return type to gzip in the header
("content-encoding", "gzip");
} else { // // Client does not support gzip
pw = ();
}
return pw;
}

The servlet code is as follows:
Copy the codeThe code is as follows:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
("utf-8");
("Content-Encoding", "gzip");
String ret = "{\"ContentLayer\":{\"title\":\"ContentLayer\"},\"PageLink\":{\"title\":\"Page Jump\"},\"WebBrowser\":{\"title\":\"Browser\"},"
+ "\"InlinePage\":{\"title\":\"Inline Page\"},\"VideoComp\":{\"title\":\"Video\"},"
+ "\"PopButton\":{\"title\":\"Content Switch\"},\"ZoomingPic\":{\"title\":\"Zooming Image\"},"
+ "\"Rotate360\":{\"title\":\"360 degree rotation\"}}";
PrintWriter pw = new PrintWriter(new GZIPOutputStream(()));
(ret);
();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
(request, response);
}

The data tracked in the proxy software is as follows:
Copy the codeThe code is 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 the codeThe code is as follows:

{"ContentLayer":{"title":"ContentLayer"},"PageLink":{"title":"Page Jump"},"WebBrowser":{"title":"Browser"},"InlinePage":{"title":"InlinePage"},"VideoComp":{"title":"Video"},"PopButton":{"title":"Content Switch"},"ZoomingPic":{"title":"ZoomingPic":{"title":"ZoomingPic"},"Rotate360":{"title":"360 degree rotation"}}