Skip to content

Instantly share code, notes, and snippets.

@richard1122
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richard1122/d7643ed74b4b207e33ca to your computer and use it in GitHub Desktop.
Save richard1122/d7643ed74b4b207e33ca to your computer and use it in GitHub Desktop.
Android HTTPURLConnection auto read gzip inputStream
public static String networkInputstreamtoString(InputStream is) throws IOException {
byte []sig = new byte[2];
final PushbackInputStream pb = new PushbackInputStream(is, sig.length);
pb.read(sig);
pb.unread(sig);
if (sig[0] == (byte) 0x1f && sig[1] == (byte) 0x8b) {
is = new GZIPInputStream(pb);
} else {
is = new BufferedInputStream(pb);
}
int len;
byte buffer[] = new byte[1024];
final StringBuilder sb = new StringBuilder();
try {
while ((len = is.read(buffer)) != -1) {
sb.append(new String(buffer, 0, len, "UTF-8"));
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment