Last Updated: September 30, 2021
·
6.952K
· spaceotech

How to Reduce The Server Response Time with GZIP Compression?

There was a time when users have patience, and they wait for result to come from the server. But now, we are living in the new era, where we need lots of information in less time. Due to this need “GZIP compression” algorithm was introduced that eliminate the characters that are already used before.

Compression is one of the effective ways to save bandwidth and speed up your site. GZIP compression is recommended to speed up JavaScript, due to problem in the older browsers. In the 21st century, most of the traffic comes from the modern browsers and people are now become tech-savvy, so it is must to use GZIP compression to speed up the mobile app. GZIP compression is also used by Google and Yahoo.

GZIP compression reduces the server response time that results in handling more request per second than blocking the request in a queue. So if you're the one, who is having a slow mobile app, then you might check your benchmark test and check if GZIP compression is on or not. There are other parameters like super caching strategy, but for this topic we are concentrating on GZIP only.

How to use GZIP compression?

Technology Used: Android

You need to pass information to server by saying that I am going to process GZIP, so send me GZIP response. And below is the code to do that:

To do that we need to give "Accept-Encoding", "gzip" in request header.

Using HttpUrlConnection:

URL url = new URL("http://www.android.com/");
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.connect();

InputStream in;

if (connection.getContentEncoding()!= null &&connection.getContentEncoding().equalsIgnoreCase("gzip"))
{
in= new GZIPInputStream(connection.getInputStream());
} else {
in = new BufferedInputStream(urlConnection.getInputStream());
}
//Read Data from Input Stream
readStream(in);

Using Apache HTTP Request:

URL url = new URL("http://www.android.com/");
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Accept-Encoding", "gzip");

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
is = new GZIPInputStream(is);
}

//Read Data from Input Stream
readStream(is);

Using volley:
You need to override these two methods on Volley request:

parseNetworkResponse

@Override
protected Response<T> parseNetworkResponse(
NetworkResponse response) {
try {
String json=null;
String contentType = response.headers.get("Content-Encoding");

if (contentType.equalsIgnoreCase("gzip")) {
   GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(new 

ByteArrayInputStream(response.data)));

//Read Data from Input Stream
json = readStream(zis);
}else{
json = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
}

  return Response.success(
                gson.fromJson(json, clazz),
                HttpHeaderParser.parseCacheHeaders(response));

}catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
 } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
 }

}

getParams

@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept-Encoding","gzip");
return params;
}
};

We have used GZIP compression in our two applications: SmartCity-311 and NDMC-311. Our SmartCity-311 is a complete mobile suite, having five different modules that allow local government to manage their work using a smartphone. It is essential to develop a speedy app, so every employee solves the problem faster.

Another is NDMC-311, which is a highly scalable cloud-based solution available on Android and iOS platform. We are providing a solution that manage, supervise and regularize the city using smartphones and it is obvious that this application has large user-base. Being a citizen and officer based application, it allows everyone to perform various tasks faster, as we have used GZIP compression.

If you want to speed up your mobile app for users to access without any interruption, then go through above process. If you are finding difficulty, Contact our Android and iOS app developer for help.