Android gzip Usage-a condensed example of a network request in development _android

Source: Internet
Author: User
Tags stringbuffer

Android GZip:

GZIP is the abbreviation for Gnuzip, it is a GNU Free software file compression program.

The gzip encoding on the HTTP protocol is a technique used to improve the performance of Web applications. The general server is equipped with this function module, the server side does not need to make changes.

When the browser supports the gzip format, the server side transmits data in gzip format.

From the HTTP technical details, that is, HTTP request header in the "Accept-encoding", "gzip", response has a return header Content-encoding=gzip

We now access from the browser to play what the site is in gzip format transmission.

But we now have an Android client that is not accessed in gzip format.

In the same way, we can add "accept-encoding", "gzip" to the Android client request header to allow the server to deliver gzip data.

The specific code is as follows.

  private string Getjsonstringfromgzip (HttpResponse response) {String jsonstring = null;
      try {InputStream is = response.getentity (). getcontent ();
      Bufferedinputstream bis = new Bufferedinputstream (IS);
      Bis.mark (2);
      Take the first two bytes byte[] Header = new byte[2];
      int result = Bis.read (header);
      Reset input stream to start position bis.reset ();
      Determines whether the gzip format int headerdata = Getshort (header); The first two bytes of the Gzip stream are 0x1f8b if (result!=-1 && headerdata = = 0x1f8b) {LOGUTIL.D ("Httptask", "Use Gzi
        Pinputstream ");
      is = new Gzipinputstream (bis);
        else {LOGUTIL.D ("Httptask", "not use Gzipinputstream");
      is = bis;
      InputStreamReader reader = new InputStreamReader (IS, "utf-8");
      char[] data = new CHAR[100];
      int readsize;
      StringBuffer sb = new StringBuffer ();
      while ((ReadSize = reader.read (data)) > 0) {sb.append (data, 0, readsize); } jsonstring = sb.tostring ();
      Bis.close ();
    Reader.close ();
    catch (Exception e) {logutil.e ("Httptask", e.tostring (), E);
    } logutil.d ("Httptask", "Getjsonstringfromgzip net output:" + jsonstring);
  return jsonstring;
  private int Getshort (byte[] Data {return (int) (DATA[0]<<8) | data[1]&0xff);
 }

Reference, note that in actual use, I found that the first two bytes of Gzip stream are 0x1e8b, not 0x1f8b. Then check the code, the error in coding, and the second byte to &0xff0x1f8b

can be referenced standard

Http://www.gzip.org/zlib/rfc-gzip.html#file-format
http://xmagicj.diandian.com/post/2011-08-08/3641381

Thank you for reading, I hope to help you, thank you for your support for this site!

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.