Comments (1)
Retrofit的请求返回Call有点累赘,这样还要自己扩张.await方法。
@GET fun getDaily(@Url url: String): Call<Daily>
直接返回Daily不香吗?
@GET suspend fun getDaily(@Url url: String): Daily
Call + .await方法Retrofit已经有自己的实现了,可以直接拿来用
suspend fun <T : Any> Call<T>.await(): T { return suspendCancellableCoroutine { continuation -> continuation.invokeOnCancellation { cancel() } enqueue(object : Callback<T> { override fun onResponse(call: Call<T>, response: Response<T>) { if (response.isSuccessful) { val body = response.body() if (body == null) { val invocation = call.request().tag(Invocation::class.java)!! val method = invocation.method() val e = KotlinNullPointerException("Response from " + method.declaringClass.name + '.' + method.name + " was null but response body type was declared as non-null") continuation.resumeWithException(e) } else { continuation.resume(body) } } else { continuation.resumeWithException(HttpException(response)) } } override fun onFailure(call: Call<T>, t: Throwable) { continuation.resumeWithException(t) } }) } }
我们要做的只是正常suspend block与try catch的异常处理
收到,感谢老铁参与对Kotlin Coroutine的探讨〜
关于以上提出的两点,在Retrofit中对Coroutines的支持,其实是在Retrofit库升级> = 2.6.0开始支持的,它内置提供了对Kotlin Coroutines的支持。目前项目中Retrofit库引用的是最新的版本(2.6.1),和自己手写了await函数,其目的是为了更好的帮助新手学习理解Kotlin +协程+ Retrofit搭配使用等。
感谢理解与支持。
from eyepetizer.
Related Issues (20)
- 滑动的时候卡顿严重 HOT 8
- 还不错 HOT 1
- 后台源码访问不了 HOT 1
- 不继续更新了吗 哈哈 HOT 2
- 视频点击全屏后,再次点击全屏按钮退出全屏,第三次点击全屏按钮时不能横屏 HOT 4
- 友盟的maven仓库地址变了 HOT 2
- 【bug】首页 -> 日报 滚动下拉后奔溃 HOT 1
- 有继续维护这个项目的计划吗,比如更新依赖等 HOT 1
- 【赞👍】项目学习问题,望不吝赐教 HOT 1
- 搜索接口 HOT 1
- author blog can not access HOT 3
- 还不错 HOT 1
- F-Droid HOT 2
- How to change language HOT 1
- 怎么登录 HOT 2
- 当我保持targetSdkVersion 32,会出现INSTALL_PARSE_FAILED_MANIFEST_MALFORMED错误,改成targetSdkVersion 30会立刻正常,这是怎么回事呢? HOT 1
- 开眼招聘
- 请问图片红色标记的东西为什么这么做 HOT 1
- SharedPreferences扩展可删除 HOT 1
- 赞赞赞 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eyepetizer.