This HTTP server sends content in the form of GZIPped ( Content-Encoding: gzip ; see http://en.wikipedia.org/wiki/HTTP_compression if you don't know what this means), so you need to wrap aUrl.openStream() in a GZIPInputStream that will unpack it for you. For example:
builder.build(new GZIPInputStream(aUrl.openStream()));
Edited to add , based on the following comment: if you donβt know in advance whether the GZIPped URL will be, you can write something like this:
private InputStream openStream(final URL url) throws IOException { final URLConnection cxn = url.openConnection(); final String contentEncoding = cxn.getContentEncoding(); if(contentEncoding == null) return cxn.getInputStream(); else if(contentEncoding.equalsIgnoreCase("gzip") || contentEncoding.equalsIgnoreCase("x-gzip")) return new GZIPInputStream(cxn.getInputStream()); else throw new IOException("Unexpected content-encoding: " + contentEncoding); }
(warning: not verified) and then use:
builder.build(openStream(aUrl.openStream()));
. This is basically equivalent to the above - aUrl.openStream() explicitly documented as a shorthand for aUrl.openConnection().getInputStream() - except that it considers the Content-Encoding header before deciding whether to wrap the stream in GZIPInputStream .
See the documentation for java.net.URLConnection .
ruakh
source share