{
“desc”: “扔物线”,
“id”: 29,
“imagePath”: “https://wanandroid.com/blogimgs/8a0131ac-05b7-4b6c-a8d0-f438678834ba.png”,
“isVisible”: 1,
“order”: 0,
“title”: “声明式 UI?Android 官方怒推的 Jetpack Compose 到底是什么?”,
“type”: 0,
“url”: “https://www.bilibili.com/video/BV1c5411K75r”
}
],
“errorCode”: 0,
“errorMsg”: “”
}
和大多数 Api 接口一样,提供了通用的 errorCode
, errorMsg
和 data
模板字段,当接口出现异常,我们可以根据状态码和消息给用户提示。
不过还有一种异常情况,即网络错误,我们无法通过 errorCode
识别
另外有一些通用的异常情况,比如用户登录过期,我们也可以统一处理
正文
要使用协程,我们需要添加以下依赖
implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1’
我们创建一个返回对象的实体
data class ResponseResult(
@SerializedName(“errorCode”) var errorCode: Int = -1,
@SerializedName(“errorMsg”) var errorMsg: String? = “”,
@SerializedName(“data”) var data: T? = null
)
banner 对象实体,省略部分属性
data class Banner(
@SerializedName(“id”) var id: Long = 0,
@SerializedName(“title”) var title: String = “”,
@SerializedName(“desc”) var desc: String = “”,
@SerializedName(“url”) var url: String = “”
)
创建 Retrofit
Api 接口,Retrofit
自 2.6 版本开始,原生支持了协程,我们只需要在方法前添加 suspend
修饰符,即可直接返回实体对象
interface IApi {
@GET(“banner/json”)
suspend fun getBanner(): ResponseResult<List>
}
创建 Api 代理对象
object Api {
private val api by lazy {
val retrofit = Retrofit.Builder()
.baseUrl(“https://www.wanandroid.com/”)
.addConverterFactory(GsonConverterFactory.create())
.client(OkHttpClient.Builder().build())
.build()
retrofit.create(IApi::class.java)
}
fun get(): IApi {
return api
}
}
在 ViewModel 中请求数据
class BannerViewModel : ViewModel(){
val mBannerLiveData = MutableLiveData<List>()
fun getBanner() {
viewModelScope.launch {
val res = Api.get().getBanner()
if (res.errorCode == 0 && res.data != null) {
mBannerLiveData.postValue(res.data)
}
}
}
}
不知道大家发现没,这样的写法是有问题的,当网络错误时,会导致 Crash,因此我们还需要对 Api 方法添加 try-catch
class BannerViewModel : ViewModel(){
val mBannerLiveData = MutableLiveData<List>()
fun getBanner() {
viewModelScope.launch {
val res = try {
Api.get().getBanner()
} catch (e: Exception) {
null
}
if (res != null && res.errorCode == 0 && res.data != null) {
mBannerLiveData.postValue(res.data)
}
}
}
}
如果还要处理登录过期的异常,还需要添加更多代码,那我们能否更加优雅地处理这些异常情况呢?
创建 apiCall
方法,统一处理异常逻辑
suspend inline fun apiCall(crossinline call: suspend CoroutineScope.() -> ResponseResult): ResponseResult {
return withContext(Dispatchers.IO) {
val res: ResponseResult
try {
res = call()
最后
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
[外链图片转存中…(img-hWHClfh2-1643541355098)]