android - 改造入队请求在 onResponse 方法中不返回 liveData
问题描述
当我使用改造请求从 web api 获取值时,然后将其分配给 liveData。但它总是在将值设置为 onResponse 中的 responseLiveData 之前返回 null。
fun fetchContents(): LiveData<String> {
val responseLiveData: MutableLiveData<String> = MutableLiveData()
val flickrRequest: Call<String> = flickrApi.fetchContents()
flickrRequest.enqueue(object : Callback<String> {
override fun onFailure(call: Call<String>, t: Throwable) {
Log.e(TAG, "Failed to fetch photos", t)
}
override fun onResponse(
call: Call<String>,
response: Response<String>
) {
Log.d(TAG, "Response received")
responseLiveData.value = response.body()
}
})
return responseLiveData
}
// then assign to the liveData in viewModel
val flickrLiveData: LiveData<String> = FlickrFetchr().fetchContents()
来自大书呆子牧场指南的这段代码,我发现它无法正常工作,所以我发出了这个错误。
解决方案
推荐阅读
- python - EnvironmentError 在公共注册表上使用 pipconf 中的私有注册表安装软件包
- python - AttributeError:模块“pyspark.sql.types”没有属性“ListType”
- reactjs - 实际道具未渲染(反应)
- html - 使用 CSS 创建自然边缘
- vim - 如何覆盖标准的“y”和“d”行为
- jmeter - 通过 JMeter 发送巨大请求后出现 SocketException
- c++ - Clang-Tidy“抑制 X 警告”是什么意思?
- linux - 表达式中的语法错误(错误标记是“INPUT (policy ACCEPT) | cut -d ' ' -f1)
- javascript - 从 Reactjs 中的 Card 组件重定向到描述页面
- python - 我使用 python 在我的计算机和我的 android 手机之间共享布尔值的最佳方式是什么?