The gzip test for Java EE and Android

Source: Internet
Author: User
Tags http post

Using gzip for data compression transfer experiments, the server is a Java EE, using the HTTP POST method for data requests.

In order to facilitate testing, just started in the environment of the Java EE to write a testcase to call the Java EE Write service, forget to write gzip decompression code, the data can be extracted normally.

Good! Since the service is ultimately available to the Android side to invoke, change to Android client to test.

Initially did not explicitly call gzip decompression, found that the data is not displayed correctly, intercepted data as follows:

?? V*j-v? O? T? Rr?q?? SR.? L?? T??? solicitation [email protected]?? 2

Next try to use the following code to extract, the result is out.

{"Resbody": "", "Rescode": 1, "resmsg": "Parse Succeeded"}

In the process, a very strange feature was found.

Determined to set a breakpoint on the request header analysis, found in the Java EE environment call, unexpectedly can not explicitly call decompression code, Apache HTTP package actually will be based on the response header is gzip, automatically call a Gzip***stream class to extract.

Anti-view Android, even if the received gzip data, but the call is Eof****stream class to go to InputStream, the output is not decompressed data.

How can you be so understanding?! It seems that inertial thinking doesn't work!

Android Decompression Code:

1        /**2 * @Title: Getjsonstringfromgzip3 * @Description: gzip uncompressed, turn into JSON string4       * @paramResponse5         * @returnString return type6        * @throws7         */8          Public StaticString Getjsonstringfromgzip (httpresponse response) {9String jsonstring =NULL;Ten         Try { OneInputStream is =response.getentity (). getcontent (); ABufferedinputstream bis =NewBufferedinputstream (IS); -Bis.mark (2); -             //take the first two bytes the             byte[] Header =New byte[2]; -             intresult =Bis.read (header); -             //reset input stream to start position - Bis.reset (); +             //determine if the gzip format -             intHeaderdata =Getshort (header); +             //the first two bytes of the Gzip stream are 0x1f8b A             if(Result! =-1 && headerdata = = 0x1f8b) { atis =NewGzipinputstream (bis); -}Else { -is =bis; -             } -InputStreamReader reader =NewInputStreamReader (IS, "Utf-8"); -             Char[] data =New Char[100]; in             intreadsize; -StringBuffer SB =NewStringBuffer (); to              while((ReadSize = reader.read (data)) > 0) { +Sb.append (data, 0, readsize); -             } theJsonstring =sb.tostring (); * bis.close (); $ reader.close ();Panax Notoginseng}Catch(Exception e) { -             Throw Newdataexception (e); the         } +  A         returnjsonstring; the}
View Code

The HTTP request header for Android should be prefixed with the following code:

1 httprequest.setheader ("accept-encoding", "gzip"); Httprequest.setheader ("Accept", "application/ Octet-stream ");

PS, it is said that now can be made into server configuration, regardless of the service side or client, do not need to explicitly call gzip plus decompression. Have time to give it a try!

The gzip test for Java EE and Android

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.