Retrofit-,BAT大厂面试题整理

// optional - GCMNetworkManager supportimplementation "androidx.work:work-gcm:2.3.4"// optional - Test helpersandroidTestImplementation "androidx.work:work-testing:2.3.4"implementation 'org.conscrypt:conscrypt-android:2.2.1'具体根据需求添加#### 2.2 请求辅助类
摘要由CSDN通过智能技术生成
// 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 {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值