android - 如何使用改造 + 协程处理错误响应
问题描述
我是协程的新手。本教程尝试使用改造 + 协程 + Jake Wharton 的 CoroutineCallAdapterFactory
但是不知道如何处理 json 响应错误。例如错误可能是这样的:
{
"code": 105,
"error": "invalid field name: bl!ng"
}
我认为在TmdbMovie类中添加代码和错误字段(并检查空字段的对象)- 这是错误的。然后 - 在TmdbViewModel中启动协程后如何使用错误字段?
解决方案
try/catch
await
正如@JakeWharton 在本期中推荐的那样
try {
youService().await()
} catch(ex: HttpException) {
// do your handling here
}
推荐阅读
- angular - 无法在 ngAfterViewInit 中获得一致的元素高度
- c - 可能是简单的按位移位问题
- algorithm - 查找树中两个给定顶点之间路径的最有效方法
- php - 如何在 laravel 中获取确切的错误位置
- c# - 如何在 ListView 中显示嵌套列表?
- macros - Clojure 风格的线程宏?
- ios - Firebase 通知仅显示在控制中心
- kubernetes - 如何通过 kubernetes 运行领事?
- java - Android Studio/Android Monitor emulator 5554-断开连接/离线/没有连接设备
- java - 正确地制定常数的条件?