Kotlin Android request client
You should look into coroutines.
Coroutines are the Promises from JavaScript and the CompleteableFutures of Java.
It's basically Kotlin handeling the callbacks for you.

suspend fun getString() : String {
    return "t"
}
suspend fun main() {
    val t = getString()
    println(t)
}
//is roughly the same as:
fun getString(lamb: (String)-&gt;Unit){
    lamb("t")
}
fun main() {
    getString{
        println(t)
    }
}


You don't really need to know the implementation, but just some rules:

suspend functions can only be called from suspend-functions (because of the hidden parameter)
Suspend functions allow you to replace lammbda's with normal code, even try catch blocks!!!

in our case, we can change makeRequest to use suspension functions:

private inline suspend fun &lt;reified T&gt; makeRequest(url: String, p: Parameters) {
    val json = buildPath(url, p)
        .httpGet()
        .awaitObjectResult(jsonDeserializer()).obj()
    Log.i("json", json.toString())
    return deserialize&lt;T&gt;(json.get("results").toString())
}


change the search with:

suspend fun search(p: Parameters) = makeRequest("$API/search", p, callback)


then in MainActivity:

class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
    override fun onDestroy() {
        cancel() // cancel is extension on CoroutineScope
    }

    //you can compare using launch with having a second thread.
    fun showSomeData() = launch { 
        try {
            val text = XkcdClient(private val main: Activity).search(..)
        } catch (e : Exception) {
            Log.i("failed", "request failed")
            Toast.makeText(
                main,
                "Request Failed!",
                Toast.LENGTH_LONG
            ).show()
        }
    }
}


extra dependencies:


implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
implementation 'com.github.kittinunf.fuel:fuel-coroutines:'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'


extra info


https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
https://kotlinlang.org/docs/reference/coroutines/coroutines-guide.html