使用Kotlin协程配合Retrofit发送请求

Retrofit2.6开始增加了对Kotlin协程的支持,可以通过suspend函数进行异步调用。本文简单介绍一下使用Kotlin协程配合Retrofit使用,发起网络请求。

导入依赖#

app的build文件中加入:

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
  • 注意依赖版本

data class#

data class ResultData<T>(
    val code: Int,
    val message: String,
    val data: T
)

Retrofit实例#

/**
 * 创建Retrofit提供API Service
 */
object RetrofitClient {

    const val BASE_URL = "http://192.168.2.194:8080/" // http://localhost:8080/

    val okHttpClient = OkHttpClient.Builder()
        .callTimeout(30, TimeUnit.SECONDS)
        .build()

    val retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttpClient)
        .build()

    val articleService = retrofit.create(ArticleService::class.java)

}

Service#

interface ArticleService {
    @GET("article/get/{id}")
    suspend fun getArticleById(@Path("id") id: Long): ResultData<Article>
}

  • 注意,请求路径前不加/,因为BASE_URL中已经加了

在ViewModel中使用#

class ArticleViewModel : ViewModel() {

    val article by lazy {
        MutableLiveData<Article>()
    }

    fun getArticle(id: Long) {
        viewModelScope.launch {
            val article = RetrofitClient.articleService.getArticleById(id)
            contentList.value = articleList.data
            Log.d("ViewPagerViewModel", "getArticle: $article")
        }
    }
}

ViewModelScope#

为了调用suspend方法,需要提供一个CoroutineScope,如果实在ViewModel中进行api请求,可以像实例中那样使用ktx提供的viewModelScope,viewModelScope可以帮助我们在viewModel的onCleared时自动进行job的cancel,否则需要在手动cancel。

不在ViewModel中使用,例如在Activity中使用时,可以如下调用

CoroutineScope(Dispatchers.Main).launch {
   // ...
}

不使用协程#

Service#

interface ArticleService {
    @GET("article/get/{id}")
    fun getArticleById(@Path("id") id: Long): Call<Article>
}

返回Call类型结果,通过enqueue插入队列等待回调处理

fun getArticle(id: Long) {
    private val api = RetrofitClient.articleService
    api.getArticleById().enqueue(object : Callback<Article> {
        override fun onResponse(call: Call<Article>, response: Response<Article>) {
            // 成功逻辑
        }
        override fun onFailure(call: Call<Article>, t: Throwable) {
            // 失败逻辑
        }
    })
}

安卓开发封装处理Retrofit协程请求的异常

posted @   sw-code  阅读(5585)  评论(0编辑  收藏  举报
编辑推荐:
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
点击右上角即可分享
微信分享提示
主题色彩