package com. example. myapplication
import retrofit2. http. GET
interface BannerService {
@GET ( "/banner/json" )
suspend fun listRepos ( ) : BaseRes< List< Data> >
}
package com. example. myapplication
import kotlinx. coroutines. flow. Flow
interface BannerApiHelp{
fun listRepos ( ) : Flow< BaseRes< List< Data> > >
}
package com. example. myapplication
import kotlinx. coroutines. flow. flow
class BannerApiHelpImp ( private val apiService: BannerService) : BannerApiHelp {
override fun listRepos ( ) = flow { emit ( apiService. listRepos ( ) ) }
}
class NetModel : ViewModel ( ) {
fun loadData ( ) : Flow< BaseRes< List< Data> > > {
return BannerApiHelpImp (
RetrofitUtils. getHttp ( ) . create ( BannerService:: class . java)
) . listRepos ( )
}
}
1.不需要配置其他adapter转换器
fun getHttp ( ) : Retrofit {
return Retrofit. Builder ( ) . baseUrl ( BaseURL) . client ( initClient ( ) )
. addConverterFactory ( MoshiConverterFactory. create ( ) )
. build ( )
}
lifecycleScope. launch {
bannerModel. loadData ( ) . collect { result ->
run {
println ( "flow 结果 ${ result } " )
}
}
}