Gzip compression is used for json data transmission in the android interface

Source: Internet
Author: User
: This article describes how to use gzip to compress json data in the android interface. if you are interested in the PHP Tutorial, refer to it.
Currently, android access interfaces use json format for data transmission (simple and easy to parse across platforms). to save bandwidth and transmission time, servers often perform gzip compression before transmission, the following are some notes.

1. for php servers, the Accept-Encoding: gzip and deflate parameters must be included in the header when accessing the interface. Otherwise, the backend will not be compressed even if gzip is compressed.

2. if the server compresses the data by gzip, you need to decompress the gzip before obtaining the data from the ap:

Public String getResponseBodyAsString (HttpResponse response) throws IOException{GZIPInputStream gzin; if (response. getEntity ()! = Null) {Header header = response. getFirstHeader ("Content-Encoding"); if (header! = Null & header. getValue (). toLowerCase (). indexOf ("gzip ")! =-1) {gzin = new GZIPInputStream (response. getEntity (). getContent (); InputStreamReader isr = new InputStreamReader (gzin, "UTF-8"); BufferedReader br = new BufferedReader (isr); StringBuilder sb = new StringBuilder (); String tmp; while (tmp = br. readLine ())! = Null) {sb. append (tmp); sb. append ("\ r \ n");} br. close (); isr. close (); return sb. toString ();} else {// Otherwise, return EntityUtils. toString (response. getEntity (), HTTP. UTF_8) ;}} else {return null ;}}


The above introduces the use of gzip compression in the transmission of json data in the android interface, including the Exception content, and hope to help friends who are interested in the PHP Tutorial.

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.