这里是视图模型
fun getUsers() = userRepository.getUsers()
这里是存储库
fun getUsers(): LiveData<Result<List<User>>> = liveData {
emit(Result.Loading)
try {
val response = userService.findUsers(perPage = 20)
Log.d("UserRepository", "getUsers: $response ")
val listUsers = response.map {
User(it.login, it.avatarUrl, it.htmlUrl)
}
emit(Result.Success(listUsers))
} catch (e: Exception) {
if (e !is CancellationException) {
Log.d("UserRepository", "getUsers: ${e.message.toString()} ")
emit(Result.Error(e.message.toString()))
} else {
Log.d("UserRepository", "getUsers: ${e.message.toString()} ")
emit(Result.Error(e.message.toString()))
}
}
}
这是服务
@GET("users") suspend fun
findUsers(@Query("per_page") perPage: Int?): ArrayList<UserResponse>
问题是当我尝试观察视图模型时,我不知道为什么会抛出取消异常
我尝试延长 okhttp 的超时时间但没有成功,预期结果是获取的数据