- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个使用 Kotlin 协程和 Retrofit 的项目。
我有这些依赖项:
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
今天我把项目中的Retrofit更新到了2.6.0。在 https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter据记载,它现在已被弃用。在 https://github.com/square/retrofit/blob/master/CHANGELOG.md#version-260-2019-06-05据记载,Retrofit 目前支持suspend
。
因此,我删除了 retrofit2-kotlin-coroutines-adapter:0.9.2
并在 Retrofit 客户端中更改了这些行:
retrofit = Retrofit.Builder()
.baseUrl(SERVER_URL)
.client(okHttpClient)
.addConverterFactory(MyGsonFactory.create(gson))
//.addCallAdapterFactory(CoroutineCallAdapterFactory()) - removed it.
.build()
运行时,第一个请求捕获异常:
java.lang.IllegalArgumentException: Unable to create call adapter for kotlinx.coroutines.Deferred<com.package.model.response.UserInfoResponse>
for method Api.getUserInfo
据我所知,我可以使用 CallAdapter.Factory()
而不是 CoroutineCallAdapterFactory()
,但它是抽象的。
如果在 Api 类中,我更改了一个在开头添加 suspend
的请求:
@FormUrlEncoded
@POST("user/info/")
suspend fun getUserInfo(@Field("token") token: String): Deferred<UserInfoResponse>
override suspend fun getUserInfo(token: String): Deferred<UserInfoResponse> =
service.getUserInfo(token)
我得到这个异常:
java.lang.RuntimeException: Unable to invoke no-args constructor for kotlinx.coroutines.Deferred<com.package.model.response.UserInfoResponse>. Registering an InstanceCreator with Gson for this type may fix this problem.
最佳答案
阅读 https://github.com/square/retrofit/blob/master/CHANGELOG.md#version-260-2019-06-05我看到了:
New: Support suspend modifier on functions for Kotlin! This allows you to express the asynchrony of HTTP requests in an idiomatic fashion for the language.
@GET("users/{id}") suspend fun user(@Path("id") long id): User
Behind the scenes this behaves as if defined as fun user(...): Call and then invoked with Call.enqueue. You can also return Response for access to the response metadata.
Currently this integration only supports non-null response body types. Follow issue 3075 for nullable type support.
我这样更改了请求:添加了 suspend
并删除了 Deferred
:
@FormUrlEncoded
@POST("user/info/")
suspend fun getUserInfo(@Field("token") token: String): UserInfoResponse
override suspend fun getUserInfo(token: String): UserInfoResponse =
service.getUserInfo(token)
然后在交互器中(或者只是在调用方法 getUserInfo(token)
时)移除 await()
:
override suspend fun getUserInfo(token: String): UserInfoResponse =
// api.getUserInfo(token).await() - was before.
api.getUserInfo(token)
更新
有一次我遇到下载PDF文件需要去掉Api类中的suspend
的情况。参见 How to download PDF file with Retrofit and Kotlin coroutines? .
关于android - 改造 2.6.0 异常 : java. lang.IllegalArgumentException : Unable to create call adapter for kotlinx. coroutines.Deferred,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56473539/
import kotlinx.coroutines.* fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
我有一个长计算的外部库。这个库就是一切,但在过早停止方面是合作的。它被包装并在一个协程中启动。 我想从调用方终止进程。协程取消是合作的,所以它不起作用。有没有办法突然终止协程? 最佳答案 非挂起的协程
我是 Kotlin/Coroutines 的新手,我注意到两种不同的使用方式 CoroutineScope . 选项 1 如下,在任何函数中: CoroutineScope(Dispatchers.D
我对协同例程比较陌生,我正在尝试获取 Launch 协同例程将完成的行为: launch(UI) { val v1 = someDeferredType val v2 = v1.a
文档说: @asyncio.coroutine Decorator to mark generator-based coroutines. This enables the generator use
我在 kotest 中使用了 Funspec 测试风格,我得到了一个由框架自动注入(inject)的 coroutineScope,如下所示。 class MyTestSpec: FunSpec()
我想尝试从一个courtine 中暂停主线程。几个问题,但首先让我向您展示我正在使用的代码块作为测试: class MainActivity : Activity(), CoroutineScope
我不确定我是否理解 purescript-coroutines 中有一个 connect 和一个 pullFrom 函数的原因。 ,以及何时使用它们。看看这些类型,它们似乎已经习惯了改变“通信方向”(
我在不同的类(class_A、class_B)中有 2 个协程(co_A、co_B)由调度程序定期调用。在执行期间的某个时候,co_B 需要一个 co_A 在其运行期间正在计算的结果。 我想做的是这样
在 document , 如果方法也用@gen.coroutine 修饰,则不需要@web.asynchronous。像这样 @web.asynchronous @gen.coroutine def
目前在 pzmq 中有多种方法来实现文档中提到的异步 IO 循环。 http://pyzmq.readthedocs.io/en/latest/api/index.html 从纯性能的角度来看,文档中
看来我误解了当有许多套接字被打开时 Windows 如何在 TIME_WAIT 中处理套接字。如果在 TIME_WAIT 中挂出太多,它只会出错。 Linux 清理旧连接并成功(至少在我的盒子上,不确
我正在尝试对我使用 Kotlin 协程制作的 Presenter 进行单元测试,这也是我第一次使用 Mockito 每当我尝试运行单元测试时,它在协程中第一次尝试对我的 View 执行任何操作时都会收
我注意到 Boost.Coroutine2 for C++ 中删除了对对称协程的支持,所以我想了解 - 两者之间有什么区别? 最佳答案 Ana Lúcia de Moura 和 Roberto Ier
在Linux System Programming, 2nd Edition书中,协程与纤维之间的差异解释如下: 协程和纤维提供了比线程更轻的执行单元(前者是编程语言结构时的名称,后者是系统结构时的名
此示例已在另一个问题中用于说明如何使用协程来编写视频游戏中的过场动画: bob.walkto(jane) bob.lookat(jane) bob.say("How are you?") wait(2
我正在寻找实现 FSM 的方法,这导致我第一次遇到协程。 我看到了几个示例(here、here 和 here),它们暗示状态机可以由单个协程实现。然而,我注意到所有这些机器的共同点是,不包括循环,它们
我试图从ViewModel异步获取所有可启动的已安装应用程序。 这是我的ViewModel类的样子: class AppInstalledViewModel(application: Applicat
如何在kotlin中像多线程一样进行多次启动 我想让 first second 永远同时工作!! 喜欢这段代码... runBlocking { // first launch{
最近我正在按照这个 CodeLabs tutorial 学习 kotlin-coroutine 。经过一些实践后,我想知道我是否可以在 java 中编写相同的代码。所以首先我在 MyKotlinFra
我是一名优秀的程序员,十分优秀!