Kotlin and Retrofit 2: Tutorial with working codes

--

This blog, I will show you how to use Kotlin and Retrofit to read data from a server. Here, I also supply together the actual code example. Other than learning the general network call related info, hopefully you’ll get a few gist on Kotlin features that could be handy.

The Example App

The tutorial here will guide you to create an App that allow you enter a keyword, and show you how many search-count it has on wiki as the image below shows. You can get the code at the bottom of the page. The explanation of how to get there in the following sections.

The Endpoint API Server

Instead of creating our own server, let’s use an existing popular API readily available, so we could focus on just creating the Android side of code. Here we use wiki provided API that given a search-string, it will return the search-count of the hit for the provided search-string.

i.e. we’ll perform a search on https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=Trump, it will return JSON result as below, with totalhits as the count we wanted.

--

--