0
我有一个应用程序调用web服务。我记录了在GZIP和GZIP之间完成此次通话所需的时间。我用5次运行应用程序5次,没有GZIP,实际上花了更长的时间与GZIP。所以我只能认为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个运行= 2923ms
随着GZIP:平均5个运行= 3179ms
好的,谢谢你的 – turtleboy