Press "Enter" to skip to content

Sending Network Request using Kotlin

Networking has always been necessary in any app. So I decided to write about doing network calls using Kotlin. So let’s start by setting up the project.

I am using Android Studio 3.0 for this tutorial, so if you want to follow along you’ll want to have that as well.
Create a new project and tick on Include Kotlin support.

Now before doing anything else, let’s setup our dependencies that we’re going to use in our project. For Networking I am going to use Fuel. Its a Networking library written completely in Kotlin, and for parsing JSON into object model, I am going to use Klaxon. I am also going to add dependencies for using Recycler View and CardView. By the end of this tutorial, we’ll be able to understand how to do a basic network request. We’re going to use this Placeholder API for that purpose.

Dependencies

After that add internet permission in your AndroidManifest.xml

Views

It’s time we start creating our layouts. In your layout folder, create a layout called recycler_view_row.xml. Which is going to represent single row of the list. Paste the following code inside your newly created xml.

Now in your activity_main.xml, do the following.

Model

There are two things we need to create, an Adapter for the RecyclerView and the Model which will help us populate the adapter.
Basically our JSON which we are fetching from the server consists of array of JSONObjects. Inside each JSONObject contains keys; id, userId, body and title. So our model will be quite simple.

Create a class and name it PostModel.

Create another class and name it PostAdapter.

In your MainActivity, create the following variables.

After that we’ll setup our RecyclerView in onCreate.

and for fetching the JSON, I have written a generic function.

How to use this function? It’s pretty straight forward. Just provide a URL and it’ll return you a JSON string or an error respectively.

I will explain the above code a little bit. What it does is that when you get the response string, we create a string builder and pass it to the Parser class object’s function parse, provided by the Klaxon library which easily cast it to JsonArray of JsonObjects, which we can cast into our model class object which is PostModel, that’s what is being done here using the .map lambda function of the JsonArray, after the mapping of objects a function named filterNotNull() is called so that all the objects which are not null can be filtered out in an array, which can then be further passed into the adapter.

NOTE: if you use the default Java classes of JSONObject and JSONArray, you won’t be able to use functions like map, flatMap, or filterNotNull. 

After everything is done, call both of the functions in your onCreate. It should look something like this.

Now run your app and see the magic. You might get the result similar to the following screenshot.

kotlin_fuel

So that’s all for this tutorial. Hope you guys liked it.
You can find the complete source code for this tutorial here.

Adiós!

 

Author: Aaqib Hussain

Aaqib is an enthusiastic programmer with the love of Swift and anything that looks like Swift i.e Kotlin. He loves writing code in Swift, and exploring new technology and platforms. He likes to listen to old music. When he is not writing code, he’s probably spend his time watching movies, tv-shows or anime, or either doing some research for writing the next article. He started Kode Snippets in 2015.

One Comment

  1. Atif
    Atif October 2, 2017

    Should be sharing screenshots of the app.

Leave a Reply

Your email address will not be published. Required fields are marked *