Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Gzip/Http supported by default?

I am using the code shown below to get Data from our server where Gzip is turned on. Does my Code already support Gzip (maybe this is already done by android and not by my java program) or do I have to add/change smth.? How can I check that it's using Gzip? For my opionion the download is kinda slow.

private static InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
          throw new IOException("Not an HTTP connection");

        try {
          HttpURLConnection httpConn = (HttpURLConnection) conn;
          httpConn.setAllowUserInteraction(false);
          httpConn.setInstanceFollowRedirects(true);
          httpConn.setRequestMethod("GET");
          httpConn.connect();

          response = httpConn.getResponseCode();
          if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
            if(in == null)
              throw new IOException("No data");
          }
        } catch (Exception ex) {
          throw new IOException("Error connecting");
        }
        return in;
      }
like image 705
OneWorld Avatar asked Feb 27 '23 07:02

OneWorld


1 Answers

Any modern http lib support Gzip compression, it's part of a standard for ages.

But you may need to send header : "Accept-Encoding: gzip"

You can check if it's really works using sniffer in your LAN, or on the Server. You can also check response headers, but that would require code changes (most likely, you will have to turn on gzip on your webserver).

Also, you may download 10Mb file of spaces. With gzip on it would be waaaaay faster :-)

like image 156
BarsMonster Avatar answered Mar 06 '23 11:03

BarsMonster