我正在使用下面显示的代码从我们打开 Gzip 的服务器获取数据。我的代码是否已经支持 Gzip(也许这已经由 android 而不是由我的 java 程序完成)还是我必须添加/更改 smth。?如何检查它是否使用 Gzip?在我看来,下载速度有点慢。
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;
}