如何将一个Android客户端,如果Tomcat7服务器GZIP COM pressed? [英] How will an Android Client if Tomcat7 server has GZIP compressed?

查看:104
本文介绍了如何将一个Android客户端,如果Tomcat7服务器GZIP COM pressed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从<一个href=\"http://stackoverflow.com/questions/9433790/cannot-com$p$pss-java-server-response-sent-to-jsp\">Answer在SO

至于GZIP COM pression,你不应该自己做。让我们的服务器做自己。

As to the GZIP compression, you shouldn't do it yourself. Let the server do itself.

修复您code删除所有手动尝试COM preSS的反应,就应该结束了,基本是这样的:

Fix your code to remove all manual attempts to compress the response, it should end up to basically look like this:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String json = createItSomehow();
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);
}

现在下面从HTTP连接器的Apache Tomcat 7的配置页面

Now the below from Apache Tomcat 7 configuration page for HTTP Connector

COM pression

的连接器可能使用HTTP / 1.1 GZIP玉米pression以试图保存
  服务器带宽。该参数可接受的值是关
  开(禁用COM pression),(允许玉米pression,这会导致文本数据
  是COM pressed),力(势力COM $在所有情况下p $ pssion),或
  数值的整数值(它等同于上,而是指定
  数据的输出之前的最小量为com pressed)。如果
  内容长度是不知道和COM pression设置为开或更
  咄咄逼人,输出也将是COM pressed。如果没有指定,这
  属性被设置为关。

The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is "off" (disable compression), "on" (allow compression, which causes text data to be compressed), "force" (forces compression in all cases), or a numerical integer value (which is equivalent to "on", but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to "on" or more aggressive, the output will also be compressed. If not specified, this attribute is set to "off".

COM pressionMinSize

如果COM pression被设置为开,那么这个属性可以被用来
  指定的数据量最小以前输出COM pressed。如果
  不指定,该属性默认为2048

If compression is set to "on" then this attribute may be used to specify the minimum amount of data before the output is compressed. If not specified, this attribute is defaults to "2048"

这意味着当组COM pression上。该数据将仅COM pressed如果它比2084更大

This implied that when compression in set to on. The data will only be compressed if it is greater than 2084.

在我的Andr​​oid客户端,我用下面的code发现如果数据是gzip的COM pressed与否

In my Android client I am using the following code to find if the data is gzip compressed or not

if ( entity.getContentEncoding() != null && "gzip".equalsIgnoreCase(entity.getContentEncoding().getValue())

我的提问

服务器是否还设置 entity.getContentEncoding()的值。的getValue()当COM pressed的数据?

Does Server also set the value of entity.getContentEncoding().getValue() when it compressed the data?

推荐答案

服务器一无所知什么实体是在你的Andr​​oid应用程序。如果gzip的是在使用Tomcat的连接器将设置适当的内容编码响应头。

The server knows nothing about whatever entity is in your android app. Tomcat's connector will set the Content-Encoding response header appropriately if gzip is in use.

另外,你的code是复杂得多,它需要的。你可以这样做:

Also, your code is more complicated than it needs to be. You can just do this:

if ( "gzip".equalsIgnoreCase(entity.getContentEncoding().getValue())

...因为没有一个NPE的机会。

...because there is no chance of an NPE.

这篇关于如何将一个Android客户端,如果Tomcat7服务器GZIP COM pressed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆