Introducing Catbug, a Threaded HTTP Request Library
May 6, 2014
A common complaint levied against older Android versions and devices is the presence of lag. The immaturity of the software combined with weak hardware multiplied by rapidly increasing user expectations equalled an inferior experience to Apple’s iOS. Using an antique smartphone today can be more painful than using an antique computer.
Yet, for all of the blame placed on the un-optimized OS and struggling hardware, another source of lag were the apps themselves. A perfect example of this is running blocking operations, such as network requests. If an app is programmed to perform such an operation on the app’s main thread, also known as the UI thread, the UI will freeze until the operation is over. In the case of a network request, it is not predictable how long the operation will take.
Obviously a frozen app is a bad user experience, so a witty developer would choose to perform the request on a different thread. The catch is knowing how to do that; fortunately, Android provides a handy class for this purpose called AsyncTask. Also helpful is the Java class HttpUrlConnection, which facilitates the request itself. Catbug is a library I developed using these classes to make it easy to make an HTTP request on a thread. Catbug creates a thread which performs a request with passed-in parameters (data keys/values, GET/POST, etc…) and returns the response to the main thread via a callback interface. It is designed to be simple to use and a lightweight addition to any (Gingerbread 2.3+) Android project.
Catbug is available on Github under the GPLv3 license. It is in need of documentation and unit-tests, which I hope to add soon!
Of course, it wouldn’t be right to pretend that this has never been done before. Other (awesome!) projects that perform similar tasks include android-async-http and http-request. They are both fantastic libraries with more robust feature-sets than catbug at the moment.