Simplifying Volley Requests — Kotlin Development
If you are in a hurry, feel free to jump to Part 2 of this story.
PART 1: THE BORING STUFF
Like any newbie in Android development, Volley was so magical when I first discovered it. But when I started working on some serious projects, volley seemed a bit difficult to handle. Everything worked perfect, as it should be, yet the code looked ugly and I was wasting a lot of time writing code that I don’t really need to. So I did some Googling and found out a few ways to customize volley request and response handling. Back then I was using Java for development. A typical volley request to a login API looked like this;
Almost every API call in my app took at least 20 lines of code, most of it boilerplate. So I changed it to something like this;
Yes, “I Know, You Know” what I did here.

I moved all those boiler plate code for Volley request to two classes, WolfRequest and WolfResponse. I’m not going to waste your time explaining how I did that, because that is irrelevant just like the image above.
With this I could, not only write less lines of code, but also it became easier to read and understand what’s going on. Like somebody said, code should be like poem. But I don’t understand most poems so I would say code should be like a children’s story: With simple words, easy to read and understand.
I used these two classes in almost all my projects in the last 18 months, then one fine day I started learning Kotlin. With each passing day I felt like, could something be this simple to write and easy to read! Soon after I got the hang of Kotlin I ported my custom request to this awesome language. I came to know about Features like Lambda only when I started learning Kotlin. I used the same in my request. This is how the request looks now;
Could it be any more simpler???
I don’t need WolfResponse interface anymore. It’s been replaced with Lambda functions. Let’s see how to do this.
PART 2: SOMEWHAT LESS BORING STUFF
You can see in the code above, WolfRequest takes 3 parameters; 1.url, 2. Function to retrieve json response, 3. Function to retrieve error message
Parameters of the API call is passed using the function POST()
This is the initial class declaration;
POST() takes variable length arguments of type Pair<String,Any>. In Kotlin you can write “key” to value for this type. eg: “name” to “Tyrion” or “age” to 39. This makes it easier to pass any number of parameters, of any type, with the http request, no need to create a HashMap<String,String> explicitly. Though it is converted to HashMap<String,String> before passing to Volley request.
result: (JSONObject) -> Unit function is invoked passing the response object when volley response is received (Wait, I’ll show the code in bit). I’m assuming all API calls to return a JSON object. If there’s an error then error: (String) -> Unit function is invoked passing an error message.
Below is the full code of the WolfRequest Rest of the code is explained using comments
For everything to work, we have to add one more line of code, in our app’s Application class onCreate() function:
class MyAwesomeApp : Application() {
override fun onCreate() {
super.onCreate()
WolfRequest.init(this)
}
}With this Volley requests in Kotlin are now made simple :)