How to use gzip to deliver data in Android

Source: Internet
Author: User
Tags file size

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

/** * Determines whether the browser supports gzip compression * @param req * @return Boolean/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; 
         /** * Creates a PrintWriter object that is output in the GZIP format, and creates an ordinary PrintWriter object if the browser does not support the gzip format. * @param req * @param resp * @return * @throws ioexception * * * publi C Static PrintWriter Creategzippw (HttpServletRequest req, HttpServletResponse resp) throws IOException {Pri  
            Ntwriter pw = null; if (Isgzipsupport (req)) {//support gzip compression pw = new PrintWriter (New Gzipoutputstream (Resp.getoutputstream ()  
                )); //Set the return type in header to gzip//view this column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/resp.setHe  
            Ader ("content-encoding", "gzip");  
            else {////client does not support gzip PW = Resp.getwriter ();  
        return PW; }

The servlet 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\": \ "inline page \"},\ "videocomp\": {\ "title\": \ "video \"}, "
            +" \ "popbutton\": {\ " Title\ ": \" content switch \ "},\" zoomingpic\ ": {\" title\ ": \" shrink enlarge diagram \ "},"
            + "\" 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:

vr+i+il-rv*,iur:rmjus}2óe/m>üì@òáineù¨ú?pàwg^nf^*tóor[àc[8n7@  
òós3ó2uèg÷t$¤+r zh¤

The actual data are 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"}}

Related Article

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.