: 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.