implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
implementation 'com.github.zyj1609wz:RetrofitFlowCallAdapter:1.1.0'
fun getHttp(): Retrofit {
return Retrofit.Builder().baseUrl(BaseURL).client(initClient())
.addCallAdapterFactory(FlowCallAdapterFactory.createAsync())
.addConverterFactory(MoshiConverterFactory.create())
.build()
}
class NetModel : ViewModel() {
fun loadData(): Flow<BaseRes<List<Data>>> {
return RetrofitUtils.getHttp().create(BannerService::class.java).listRepos()
}
}
interface BannerService {
@GET("/banner/json")
fun listRepos(): Flow<BaseRes<List<Data>>>
}
val bannerModel = ViewModelProvider(this)[NetModel::class.java]
lifecycleScope.launch {
bannerModel.loadData().collect { result ->
run {
println("flow 结果${result}")
}
}
}