Use gzip in Android to transmit instance code

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.