【问题标题】:GZIP has no effect on web callGZIP 对网络通话没有影响
【发布时间】:2012-11-05 16:07:16
【问题描述】:

我有一个调用网络服务的应用程序。我已经记录了使用和不使用 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 毫秒

【问题讨论】:

    标签: android http gzip


    【解决方案1】:

    在时间安排上至少有两个主要贡献:

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

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

    时间可能会在 ADSL、WLAN 或直接以太网连接之间发生变化。

    【讨论】:

      猜你喜欢
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 2015-04-11
      • 2011-02-27
      • 2019-01-12
      相关资源
      最近更新 更多