2012-11-05 42 views
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

回答

1

有在时序至少两个主要的贡献:

  • 客户端:连接速度与解码速度
  • 服务器端:连接速度与编码速度的比较

gzip编码在服务器端可以是静态或动态的。对于某些内容来说,将查询数据存储为已压缩格式是有意义的。对于某些内容无法完成,服务器可能会占用“压缩引擎”。

ADSL,WLAN或直接以太网连接之间的时间可能会改变。

+0

好的,谢谢你的 – turtleboy