HTML browsers, for the longest time, have had support for downloading and expanding Gzip content. This is an ideal way to compress the size of HTML pages because gzip is at its best when compressing text. As you can imagine, compression is even more critical when we look at consuming XML over the mobile network.
Web Servers like Apache will automatically compress static request for HTML pages using gzip if the request includes the following header:
Unfortunately, web services are typically not static content. Webservice are usually served up by application servers that don’t implicitly acknowledge the header. As such, compressing the content is left to the developer to take care of “by hand”. Luckily most modern server-side options come with everything you need to compress your data feeds.
If you are a Java developer, as I am, you may notice that the default JDK comes with support for GZipInputStream and GZipOutputStream classes. Below is a simple example of how you could use the GZip support in Java to compress data written to an HttpServletResponse’s OutputStream:
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
String xml = doSomethingToGenerateXML();
// Get the response OutputStream ...
OutputStream out = response.getOutputStream();
// Create a GZipOutputStream using the response OutputStream
GZipOutputStream gzip = new GZipOutputStream(out);
// Now write out your XML ...
// ... and close up
Presto, See how easy that was! Now your job isn’t over yet, this is an iPhone tip after all! The magic to using this compressed content in an iPhone application is to simply tell it that the content is compressed using gzip … and it will take care of the rest! You don’t even need to expand the content before you use it because NSURLConnection will do that for you.
To ensure that NSURLConnection understands that the content was compressed with Gzip, be sure to add the following header in the HttpServletResponse:
So back in the servlet example above, you can add this header as follows:
The gains you will get in reduce data packets will translate into a much more responsive network application, a cheaper phone bill, and a grateful user … so all you WebService API providers out there … COMPRESS YOUR XML!!