Skip to content

Instantly share code, notes, and snippets.

@mitraman
Created August 27, 2012 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mitraman/3491254 to your computer and use it in GitHub Desktop.
Save mitraman/3491254 to your computer and use it in GitHub Desktop.
Android/Java gzip request
private void getCompressedResource(String urlString) throws IOException {
// NOTE: Android's HTTPURLConnection automatically adds the "accept-encoding: gzip" header to requests and deflates responses.
// This code sample demonstrates how you would implement this feature if it wasn't handled for you automatically.
URL url = new URL(urlString);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setRequestProperty("accept-encoding", "gzip");
conn.setRequestMethod("GET");
InputStream is = new BufferedInputStream(conn.getInputStream());
// Note: If the library did not automatically deflate the response, we would need to use a GZipInputStream on the response.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment