我有一个调用网络服务的应用程序。我已经记录了使用和不使用 GZIP 完成此呼叫所需的时间。我使用 GZIP 运行该应用程序 5 次,不使用 GZIP 运行 5 次,实际上使用 GZIP 需要更长的时间。所以我只能认为 GZIP 没有效果,或者我实施得不好。任何想法为什么没有变化?
public String connect(String url) {
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Accept-Encoding", "gzip");
// Execute the request
HttpResponse response;
try {
long start = System.currentTimeMillis();
response = httpclient.execute(httpget);
// Examine the response status
Log.i(TAG, response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// A Simple JSON Response Read
//InputStream instream = entity.getContent();
result = convertStreamToString(instream);
Log.i(TAG, result);
// A Simple JSONObject Creation
//json = new JSONObject(result);
// Closing the input stream will trigger connection release
instream.close();
long end = System.currentTimeMillis();
long elapsed = (end - start);
Log.e(TAG, "web call took ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" + elapsed);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
.
结果:
没有 GZIP:平均 5 次运行 = 2923 毫秒
使用 GZIP:平均 5 次运行 = 3179 毫秒