Итак, у меня есть следующая функция, которая с помощью клиента Ktor выполняет базовый запрос на получение списка пользователей:
Код: Выделить всё
suspend fun createRequest(): List? {
return withContext(Dispatchers.IO) {
try {
val client = HttpClient(CIO)
val response: HttpResponse = client.get("http://10.0.2.2:9999/users")
client.close()
val str = response.readText()
val itemType = object : TypeToken() {}.type
Gson().fromJson(str, itemType)
} catch (e: Exception) {
null
}
}
}
Код: Выделить всё
runBlocking {
val res = async {createRequest()}
val users = res.await()
Log.v("_APP_", users.toString())
}
Источник: https://stackoverflow.com/questions/680 ... coroutines
Мобильная версия