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!