Reputation: 19102
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?
Upvotes: 0
Views: 369
Reputation: 20882
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.
Upvotes: 1