How will an Android Client if Tomcat7 server has GZIP compressed?

点点圈 提交于 2019-12-11 20:26:59

问题


This is from a Answer on SO

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

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);
}

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

compression

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".

compressionMinSize

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"

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

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())

My Question

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


回答1:


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.

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

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

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



来源:https://stackoverflow.com/questions/11458234/how-will-an-android-client-if-tomcat7-server-has-gzip-compressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!