@

前言

Retrofit 从 2.6.0 版本开始, 内置了对 Kotlin Coroutines 的支持. 我们统一处理异常及响应状态码, 使用DSL 让代码更加漂亮整洁

先导包:

//协程
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3' // Retrofit
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.2.0"

一、准备工作

1.BaseResp

统一的返回实体, 包含 code, msg, data; 这种服务器响应结构比较常见

data class BaseResp<T>(
// 响应状态码
var code: Int = -1,
// 响应信息
var msg: String = "",
// 数据实体
var data: T? = null
)

2.Api

这里是一个验证码请求接口, 使用的 Json 提交参数

interface Api {
@POST("api/getCode")
suspend fun getCode(@Body body: RequestBody): BaseResp<JsonObject?>
}

3.ApiManager

使用 枚举单例模式; 包括 初始化 Retrofit, OkHttpClient, 添加请求Token, 请求日志打印.

/**
* Retrofit 管理类;
*/
enum class ApiManager {
INSTANCE;
private val retrofit: Retrofit
val mApi: Api
private val mMediaTypeJson: MediaType? init {
retrofit = Retrofit.Builder()
.baseUrl("https://... 服务器地址")
.client(initOkhttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build()
mApi = retrofit.create(Api::class.java)
mMediaTypeJson = "application/json; charset=utf-8".toMediaTypeOrNull()
} private fun initOkhttpClient(): OkHttpClient {
return OkHttpClient.Builder()
.retryOnConnectionFailure(true) //重试
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.addNetworkInterceptor(initTokenInterceptor())
.addInterceptor(initLogInterceptor())
.build()
} private fun initTokenInterceptor(): Interceptor = object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
// 为请求添加 Token
val token: String = SpUtils.getToken()
val originalRequest = chain.request()
return if (token.isNullOrEmpty()) {
chain.proceed(originalRequest)
}else {
token = "Bearer $token"
val updateRequest = originalRequest.newBuilder().header("token", token).build()
chain.proceed(updateRequest)
}
}
} /*
* 日志拦截器
* */
private fun initLogInterceptor(): HttpLoggingInterceptor {
if(!BuildConfig.DEBUG){ //生产环境不打日志
return HttpLoggingInterceptor()
} val interceptor = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Log.i("Retrofit_network", message)
}
})
interceptor.level = HttpLoggingInterceptor.Level.BODY
return interceptor
} // 请求数据转 Json 形式;
open fun getJsonBody(data: Any): RequestBody {
val strEntity = mGson.toJson(data)
return strEntity.toRequestBody(mMediaTypeJson)
}
}

二、开始使用

1.简单使用

以下代码在 Activity 中;

lifecycleScope.launch {
val param = ApiManager.INSTANCE.getJsonBody(mapOf("phone" to "13333333333", "codeType" to "1"))
val data = ApiManager.INSTANCE.mApi.getCode(param).data
binding.tvTitle.text = data.toString()
}

是不是非常简单, 协程内除了准备参数 就只剩两行代码.

上面代码并没有处理异常, 及请求失败的情况. 下面我们就开始封装请求

2.DSL

class RetrofitDSL<T> {
internal lateinit var api: (suspend () -> BaseResp<T?>)
private set
internal var onSuccess: ((BaseResp<T?>) -> Unit)? = null
private set
internal var onFailed: ((msg: String?, code: Int) -> Unit)? = null
private set
internal var onComplete: (() -> Unit)? = null
private set /**
* 获取数据
* @param block (T) -> Unit
*/
fun api(block: suspend () -> BaseResp<T?>) {
this.api = block
} /**
* 获取数据成功
* @param block (T) -> Unit
*/
fun onSuccess(block: (BaseResp<T?>) -> Unit) {
this.onSuccess = block
} /**
* 获取数据失败
* @param block (msg: String, errorCode: Int) -> Unit
*/
fun onFailed(block: (msg: String?, code: Int) -> Unit) {
this.onFailed = block
} /**
* 访问完成
* @param block () -> Unit
*/
fun onComplete(block: () -> Unit) {
this.onComplete = block
}
}

看到 onSuccess, onFailed 是不是有熟悉的感觉!

3.扩展函数

我们需要创建一个 .kt 文件; 为 协程作用域 添加扩展函数

fun <T> CoroutineScope.retrofit(
dsl: RetrofitDSL<T>.() -> Unit
) {
launch {
val retrofitDsl = RetrofitDSL<T>()
retrofitDsl.dsl()
try {
val result = retrofitDsl.api()
when(val code = result.code){
200 -> retrofitDsl.onSuccess?.invoke(result)
... // 其他响应码
else -> retrofitDsl.onFailed?.invoke(result.msg, code)
}
} catch (e: Exception) {
retrofitDsl.onFailed?.invoke(e.message, -10)
}
... // 其他异常类型
finally {
retrofitDsl.onComplete?.invoke()
}
}
}

这里 只写了 Exception; 需要判断多种异常的话, 往 catch 后面加就行了. 同样, 响应码也随便加, 想怎么处理就怎么处理.

4.请求发起

以下代码在 ViewModel 中;

// 注意: <JsonObject> 与 Api 中配置的请求响应结果类型一致 BaseResp<JsonObject?>
viewModelScope.retrofit<JsonObject> {
api { ApiManager.INSTANCE.mApi.getCode(param) }
onSuccess { // it = BaseResp<JsonObject?>
_number.set(it.data?.getAsJsonPrimitive("code")?.asString)
}
onFailed { msg, _ ->
Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show()
}
onComplete {
Toast.makeText(getApplication(), "完事了", Toast.LENGTH_SHORT).show()
}
}

onSuccess, onFailed, onComplete 都可以不写, 想写谁就写谁;

// 不关心请求结果的话; 一个 api 就够了
viewModelScope.retrofit<JsonObject> {
api { ApiManager.INSTANCE.mApi.getCode(param) }
}

总结

好吧, 没有总结

Kotlin Coroutine(协程): 四、+ Retrofit的更多相关文章

  1. Kotlin Coroutine(协程): 一、样例

    @ 目录 前言 一.直接上例子 1.延时任务. 2.异步任务 3.并行任务: 4.定时任务: 总结 前言 你还在用 Hanlder + Message? 或者 AsyncTask? 你还在用 Rxja ...

  2. Kotlin Coroutine(协程): 二、初识协程

    @ 目录 前言 一.初识协程 1.runBlocking: 阻塞协程 2.launch: 创建协程 3.Job 4.coroutineScope 5.协程取消 6.协程超时 7.async 并行任务 ...

  3. Kotlin Coroutine(协程): 三、了解协程

    @ 目录 前言 一.协程上下文 1.调度器 2.给协程起名 3.局部变量 二.启动模式 CoroutineStart 三.异常处理 1.异常测试 2.CoroutineExceptionHandler ...

  4. python并发编程之gevent协程(四)

    协程的含义就不再提,在py2和py3的早期版本中,python协程的主流实现方法是使用gevent模块.由于协程对于操作系统是无感知的,所以其切换需要程序员自己去完成. 系列文章 python并发编程 ...

  5. Coroutine(协程)模式与线程

    概念 协程(Coroutine)这个概念最早是Melvin Conway在1963年提出的,是并发运算中的概念,指两个子过程通过相互协作完成某个任务,用它可以实现协作式多任务,协程(coroutine ...

  6. Android中的Coroutine协程原理详解

    前言 协程是一个并发方案.也是一种思想. 传统意义上的协程是单线程的,面对io密集型任务他的内存消耗更少,进而效率高.但是面对计算密集型的任务不如多线程并行运算效率高. 不同的语言对于协程都有不同的实 ...

  7. coroutine协程

    如果你接触过lua这种小巧的脚本语言,你就会经常接触到一个叫做协程的神奇概念.大多数脚本语言都有对协程不同程度的支持.但是大多编译语言,如C/C++,根本就不知道这样的东西存在.当然也很多人研究如何在 ...

  8. [Unity-22] Coroutine协程浅析

    1.概念解释 协程并非一个独立的线程.在Unity中.全部的语句都是在一个线程中运行的,也就是说.Unity是单线程的(详细的能够參见http://blog.csdn.net/alexander_xf ...

  9. Coroutine 协程

    https://en.wikipedia.org/wiki/Coroutine Coroutines are computer program components that generalize s ...

随机推荐

  1. 处理SpringMVC中遇到的乱码问题

    乱码在日常开发写代码中是非常常见的,以前乱码使用的是通过设置一个过滤器解决, 现在可以使用SpringMVC给提供的过滤器,在web.xml设置,这比我们自己写的过滤器强大的的多. 注意:每次修改了x ...

  2. 自动驾驶传感器比较:激光雷达(LiDAR) vs. 雷达(RADAR)

    自动驾驶传感器比较:激光雷达(LiDAR) vs. 雷达(RADAR) 据麦姆斯咨询报道,2032年全球范围内自动驾驶汽车的产量将高达2310万辆,未来该市场的复合年增长率(CAGR)高达58%.届时 ...

  3. 基于自动驾驶车辆的NVIDIA-TensorRT推理实时优化

    基于自动驾驶车辆的NVIDIA-TensorRT推理实时优化 Optimizing NVIDIA TensorRT Conversion for Real-time Inference on Auto ...

  4. C++标准模板库(STL)——queue常见用法详解

    queue的定义 queue<typename> name; queue容器内元素的访问 由于队列本身就是一种先进先出的限制性数据结构,因此在STL中只能通过front()来访问队首元素, ...

  5. python+selenium_鼠标事件

    引言--在实际的web产品测试中,对于鼠标的操作,不单单只有click(),有时候还要用到右击.双击.拖动等操作,这些操作包含在ActionChains类中. 一.ActionChains类中鼠标操作 ...

  6. ipconfig提示不是内部或外部命令

    昨天因为公司断网,重新连上之后ip地址变了,于是就想看看现在的ip是什么 输入ipconfig,回车 提示不是外部和内部命令,是因为系统在本路径下未找到ipconfig.exe系统,所以无法识别ipc ...

  7. 一文读懂高速PCB设计跟高频放大电路应用当中的阻抗匹配原理

    这一期课程当中,我们会重点介绍高频信号传输当中的阻抗匹配原理以及共基极放大电路在高频应用当中需要注意的问题,你将会初步了解频率与波长的基础知识.信号反射的基本原理.特性阻抗的基本概念以及怎么样为放大电 ...

  8. 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题

    一切为了安全,所有的云上资源如支持内网资源访问,则都可以加入虚拟网络 问题描述 使用Azure Function处理Storage Account中Blob 新增,更新,删除等情况.Storage A ...

  9. 『无为则无心』Python基础 — 14、Python流程控制语句(while循环语句)

    目录 1.什么是循环结构 2.while循环 (1)while循环的语法 (2)while循环的应用 3.while语句的死循环 4.break和continue 5.while循环嵌套 (1)应用场 ...

  10. R语言读取文件

    1.R语言读取文件,文件类型为.txt 直接使用read.table()即可,若不知道当前的工作目录,可以使用函数getwd()来查看 2.R语言读取文件,文件类型为.xlsx 方法一:可以把excl ...