I am new to android & kotlin coroutine. I have been working with okhttp API lib with coroutine. Below is the code:
Application extended Class:
class App: Application() {
companion object {
val SCOPE = CoroutineScope(Dispatchers.Main)
}
}
In MainActivity class:
App.SCOPE.launch {
val results = Api.get(url)
UpdateUI(results)
}
Api class:
suspend fun get(url: String): String = withContext(Dispatchers.IO) {
try {
val data = httpClient.newCall(request).execute()
return@withContext data.body!!.string()
} catch (e: Exception) {
throw java.lang.Exception(e.message)
}
}
This is working fine but is this the best way to go?