Reducing Network Data in Android App

Giovanka Bisano
Kargo Technologies
Published in
5 min readDec 30, 2020

Among many ways of reducing Http Call in Android, the most basic concept is using if-modified-since inside the HTTP request header. if-modified-since is an additional request header that requires date as its parameter.

What does this header actually do? When you use this header, then you might get a new kind of response code, which is 304(Not Modified)(usually you get 200(OK) status code indicating your request was successful). The 304(Not Modified) response code will tell you that your object (basically anything that you fetch) was not modified. That response code may open many possibilities to improve your app performance, for example you only update your local database when data from server changes instead of update your local database everytime user open your app.

Let’s play with a use case.

“Business / Legal team want to show Terms and Condition in the application so that the customer can see the Terms and Condition to understand the relation and how the business work.”

“The Terms and Condition can be changed in a sudden manner by Business / Legal team, and every changes must be reflected immediately without updating the app”

With that use case in mind, we can solve that by storing Terms and Condition text inside .txt / .json file and save that file to a server (can use S3 bucket). Then our application can query the text via HTTP request, save the text to local database, then show the text in our application. We will us if-modified-since header so that we only download the text only if the text get changed from server.

We did this with a consideration that the file may have a very big size depends on how long the text will be. Remember, reducing processes and network usage is very important for every mobile app!

So this is what we gonna do in step by step:

  1. Query TermsAndCondition.txt file from S3 bucket.
  2. Store that txt file into local storage. So the app only shows Terms and Condition text from a single source of truth (local storage).
  3. Store Last-Modified date to local storage. So we can use this date inside if-modified-since request header in the future.
  4. Check whether the TermsAndCondition.txt have been modified or not. If it have been modified, then update the local storage to maintain “single source of truth” principle.

Simple request without If-Modified-Since header

In Android, we are able to create a HTTP request simply by passing URL inside our Request:

For example, I query a text file from S3 bucket without If-Modified-Since header (like my code above),

Response without if-modified-since request header

It results 200(OK) status code, we also get 3025 content length (It means that the request fetched 3025 text characters and it have 4KB file size).

In response header, we can see that it have Last Modified value “Tue, 04 Aug 2020 10:46:37 GMT”. It means that our object was created (or modified) from 04 August 2020. This Last Modified value need to be saved in order to use if-modified-since header in the upcoming request. By this far, we already get 3 keypoints:

  1. If you don’t use if-modified-since header, then the response will return 200(OK)(assuming the request is success).
  2. If you use if-modified-since header, and you pass the date argument before Last Modified date, then the response will return 200(OK)(assuming the request is success).
  3. If you use if-modified-since header and you pass the date argument same or after Last Modified, then the response will return 304(Not Modified)(assuming the request is success).

Create a request with If-Modified-Since header

We able to decrease HTTP request using if-modified-since header. We just need to add that header into our request. Your code will be something like this.

Response with if-modified-since request header

As you can see from screenshot above, we decreased our HTTP response because we use if-modified-since header and pass the Last-Modified date that we get from the first HTTP request (Tue, 04 Aug 2020 10:46:37 GMT) as the parameter.

We get contentLength = 0 and status code 304(Not Modified). It means since 4 August 2020, our object have not yet modified, thus the HTTP didn’t return any content to it response.

FAQ:
“What is the date format that we need to pass in if-modified-since header?”
- Just put the “Last-Modified” value that you get from Response Header in the first request (see the first screenshot). Don’t reformat the date value that you get from first request.

Final Code

With that example use case, our code must save txt file from server to local DB and save again txt file from server IF the content from server changed.

Conclusion

Yes, we can reduce processes & network call using if-modified-since response header.
The first thing that we need to do is to make a simple Http request and save Last-Modified date from response header inside mobile app local storage. By the next time we do the same request, we add if-modified-since to our request header and fill the value with Last-Modified date from previous request. Then we will get 2 response assuming our request is success, which are 200 and 304 request code.

When the object was modified, we will get 200(OK).
When the object was not modified, we will get 304 (Not Modified).

This is just a concept, this is applicable for every platform, not only in Android.

--

--

Kargo Technologies
Kargo Technologies

Published in Kargo Technologies

Insightful stories of KargoTech Engineering, Product, and Culture

Giovanka Bisano
Giovanka Bisano

Written by Giovanka Bisano

Technical Lead at Kargo Technologies | Google Associate Android Developer Certified