Use gzip compression in the transfer of JSON data in the Android interface

Source: Internet
Author: User
Tags php server
The current Android Access interface transmits data more in JSON format (simple and easy to parse across platforms), in order to save bandwidth and transfer time the server will often be gzip compressed before transmission, here are a few points to note.

1. For PHP server, the interface must be in the header with parameters Accept-encoding:gzip, deflate, or even if the background does gzip compression, will not be real compression.

2. If the data is gzip compressed by the server, the data needs to be gzip decompressed before the AP obtains the data:

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 gzipinput                Stream (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 normally returns return Entityutils.tostring (Response.getentity (), HTTP.            UTF_8);        }} else {return null; }    }


The above describes the Android interface in the transfer of JSON data using gzip compression, including the exception aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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