Android Flow遇见Retrofit网络请求实践
生活随笔
收集整理的這篇文章主要介紹了
Android Flow遇见Retrofit网络请求实践
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/121754941
本文出自【趙彥軍的博客】
文章目錄
- 前言
- RetrofitFlowCallAdapter
- 自定義flow 構建類
- Flow多個請求合并
- 合并請求 zip
- 合并請求 combine
- Flow網絡重試 retryWhen
系列文章推薦:
Android Flow遇見Retrofit網絡請求實踐
Android Kotlin協程和Retrofit結合使用
Retrofit 注解參數詳解
前言
最近項目要從 Rxjava 切到 Flow ,探索最佳實踐,于是就有了這篇文章
RetrofitFlowCallAdapter
添加 RetrofitFlowCallAdapter
github地址:https://github.com/zyj1609wz/RetrofitFlowCallAdapter
//添加maven地址 allprojects {repositories {maven { url 'https://jitpack.io' }} }//添加依賴 implementation 'com.github.zyj1609wz:RetrofitFlowCallAdapter:1.1.0'在 Retrofit 初始化的時候加入:
val retrofit = Retrofit.Builder().baseUrl("https://api.github.com").addCallAdapterFactory(FlowCallAdapterFactory.createAsync()).addConverterFactory(GsonConverterFactory.create()).build()定義接口
interface ApiService {//返回 Body @GET("users/{user}/repos")fun listReposWithBody(@Path("user") user: String): Flow<List<Repo>>//返回 Response@GET("users/{user}/repos")fun listReposWithResponse(@Path("user") user: String): Flow<Response<List<Repo>>> }創建 ApiService 實例:
private val service: ApiService = retrofit.create(ApiService::class.java)使用姿勢一:
lifecycleScope.launch {service.listReposWithResponse("zyj1609wz").flowOn(Dispatchers.IO).catch {//處理異常it.printStackTrace()}.collect {//處理結果val list = it.body()}}使用姿勢二:使用 flow 的 launchIn 函數可以進一步簡化
service.listReposWithBody("zyj1609wz").catch {//處理異常it.printStackTrace()}.onEach {//處理結果val item = it[0] }.launchIn(lifecycleScope).start()自定義flow 構建類
首先創建函數的自定義 Flow 構建工具類
fun <T> getFlow(block: suspend () -> T): kotlinx.coroutines.flow.Flow<T> {return flow {emit(block())} }ApiService 的掛起接口聲明
interface ApiService {@GET("users/{user}/repos")suspend fun listReposWithBody(@Path("user") user: String): List<Repo> }使用:
getFlow {service.listReposWithBody("zyj1609wz")}.catch {it.printStackTrace()}.onEach {val item = it[0]}.launchIn(lifecycleScope).start()Flow多個請求合并
合并請求 zip
//第一個網絡請求val request1 = getFlow {service.listReposWithBody("zyj1609wz")}//第二個網絡請求val request2 = getFlow {service.listReposWithBody("zyj1609wz")}//兩個請求并行request1.zip(request2) { response1, response2 ->Pair(response1, response2)}.catch {it.printStackTrace()}.onEach {val list1 = it.firstval list2 = it.second}.launchIn(lifecycleScope).start()如果是兩個請求合并,那么用 zip 操作符就行了。但是 zip 也有一個問題,只支持兩個請求并行。如果是3個請求,或者更多的請求,就需要用到 combine
合并請求 combine
//第一個網絡請求val request1 = getFlow {service.listReposWithBody("zyj1609wz")}//第二個網絡請求val request2 = getFlow {service.listReposWithBody("zyj1609wz")}//第三個網絡請求val request3 = getFlow {service.listReposWithBody("zyj1609wz")}//3個請求并行combine(request1, request2, request3) { response1, response2, response3 ->//合并結果mutableListOf(response1, response2, response3)}.catch {//處理異常it.printStackTrace()}.onEach {//處理結果}.launchIn(lifecycleScope).start()Flow網絡重試 retryWhen
retryWhen { cause, attempt ->//attempt是重試次數,從0開始//返回值是 boolean 類型,true 繼續重試,false 停止重試attempt < 2 }下面我們寫一個重試兩次的場景
getFlow {service.listReposWithBody("zyj1609wz")}.retryWhen { cause, attempt ->attempt < 2}.catch {//處理異常}.onEach {//處理結果}.launchIn(lifecycleScope).start()總結
以上是生活随笔為你收集整理的Android Flow遇见Retrofit网络请求实践的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Retrofit 注解参数详解
- 下一篇: Android Kotlin Flow