// optional - GCMNetworkManager support
implementation "androidx.work:work-gcm:2.3.4"
// optional - Test helpers
androidTestImplementation "androidx.work:work-testing:2.3.4"
implementation 'org.conscrypt:conscrypt-android:2.2.1'
具体根据需求添加
#### 2.2 请求辅助类
* 状态管理
enum class Status {
SUCCESS,
ERROR,
LOADING
}
* 请求结果处理类
class Resource(val status: Status, val data: T?, val message: String?) {
companion object {
fun success(data: T?) = Resource(Status.SUCCESS, data, null)
fun error(msg: String?, data: T?) = Resource(Status.ERROR, data, msg)
fun loading(data: T?) = Resource(Status.LOADING, data, null)
}
}
#### 2.3 使用Retrofit 创建API 接口、接口帮助类
> 将接口管理和请求放在不同的类文件中,方便管理
* API 接口
interface ApiService {
@GET("{page}")
suspend fun getGirls(@Path(“page”) page: Int): Girls
}
数据类将传到Demo中 [Retrofit + OkHttp3 + coroutines + LiveData打造一款网络请求框架]( )
* API 接口类调用辅助类
class ApiHelper(private val apiService: ApiService) {
suspend fun getGirls() = apiService.getGirls(1)
}
#### 2.4 创建Retrofit及OkHttp等网络框架请求帮助类
object ServiceCreator {
private val okHttpClient by lazy { OkHttpClient().newBuilder() }
private val retrofit: Retrofit by lazy {