• 9

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to make an API request in Kotlin?

I am extremely new to Kotlin and APIs in general and can't find the syntax to create an API request using this language. I am creating a mobile version of a website so I'm using Android Studio to create a new UI for an already established backend. What are the steps and syntax to creating a request? Any help is deeply appreciated.

Once you have set your Android Studio to use Kotlin is pretty simple to do a REST call, and it's pretty much the same logic as with Java.

Here's an example of a REST call with OkHttp:


dependencies {
    implementation 'com.squareup.okhttp3:okhttp:3.8.1'


<uses-permission android:name="android.permission.INTERNET" />


class MainActivity : AppCompatActivity() {

    private val client = OkHttpClient()

    override fun onCreate(savedInstanceState: Bundle?) {


    fun run(url: String) {
        val request = Request.Builder()

        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {}
            override fun onResponse(call: Call, response: Response) = println(response.body()?.string())

Below are a few more complicated examples with other libraries:

  • 48
Reply Report

you can use Retrofit or AsyncTask , example of AsyncTask :

class getData() : AsyncTask<Void, Void, String>() {
    override fun doInBackground(vararg params: Void?): String? {

    override fun onPreExecute() {

    override fun onPostExecute(result: String?) {

for Retrofit check this awsome tutorial

  • 5
Reply Report

I have create a sample API call using retrofit 2. Firstly, add these libraries in gradle

implementation "com.squareup.retrofit2:retrofit:2.3.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"

then create a class to configure Retrofit 2, say Connect.kt

class Connect {

    companion object {

        private fun getRetrofit(Url:String):Retrofit {
            return Retrofit.Builder()

        fun getApiData():Retrofit{
            val retrofitApi = getRetrofit(Url.BASE_URL)
            return retrofitApi

        fun callApi():CallApi{
            val retrofitCall = getApiData()
            return retrofitCall.create(CallApi::class.java)


I have created Url in Url class say Url.kt

class Url {
    companion object {
        const val BASE_URL = "your base url"
        const val URL = "your url"

Created an interface for Api call

    interface CallApi {

//query needed if there is any query
        fun getApi(@Query("limit") limit: Int):
//model class is needed                

Create a model class according to your response, sample response is

    "data": {
        "children": [
                "data": {
                    "author": "",

for creating its model class, create an object say, Model

object Model {
    data class Result(val data: Data)
    data class Data(val children: List<Children>)
    data class Children(val data: Datas)
    data class Datas(val author: String,val thumbnail: String,val title: String)

Then create a boiler plate class to perform data fetch from api which can be called from any activity

class ApiData {
    companion object {
        const val count = 10
        val api by lazy { Connect.callApi() }
        var disposable: Disposable? = null
        fun apiData(callback:Response){
            disposable = api.getApi(count)
                    .subscribe ({
                        result ->
                    }, { error ->


    interface Response {
        fun data(data:Model.Result,status:Boolean)

now it can be called from activity like,

ApiData.apiData( object :ApiData.Response{
    override fun data(data: Model.Result, status: Boolean) {
            val items:List<Model.Children> = data.data.children

  • 2
Reply Report