6.10

网络请求(Retrofit + Coroutine)
接入网络API:
kotlin
// 1. 定义接口
interface ApiService {
@GET("users")
suspend fun getUsers(): List
}
// 2. 发起请求
lifecycleScope.launch {
try {
val users = RetrofitClient.api.getUsers()
adapter.data = users
} catch (e: Exception) {
Toast.makeText(this@MainActivity, "网络错误", Toast.LENGTH_SHORT).show()
}
}
注意:
不要忘记在AndroidManifest.xml中添加网络权限

posted @ 2025-06-10 14:11  也祝她开心  阅读(8)  评论(0)    收藏  举报