android - 改造寻找不存在的参数?
问题描述
我正在使用改造在我的简单 API 调用中生成 POST 请求:
interface IRetrofitCommService {
@POST("pushnotifications/{entityId}")
suspend fun getNotificationsAsync(
@Path("entityId") entityId: Long,
@Body model: GetNotificationsDto
): Response<List<NotificationData>>
@POST("pushnotifications")
suspend fun registerDeviceAsync(@Body model: RegisterEntityDto):
Response<RegisterEntityResultDto>
}
请注意,在第二次调用中,我只有 1 个参数,它标有@Body
注释。然而,当我尝试使用网络调用时,我得到了这个异常:没有为参数 2 找到注释
这是我创建呼叫的工厂:
object RetrofitFactory {
const val BASE_URL = "https://localhost:5051/api/"
fun createService(): IRetrofitCommService {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(IRetrofitCommService::class.java)
}
}
这是有问题的DTO:
data class RegisterEntityDto(val name: String, val eventType: Short, val clientId: String)
那么为什么要寻找第二个参数呢?我错过了什么?
解决方案
我对改造不是很熟悉,我在这里做一个有根据的猜测,但在评论中的简短讨论之后,似乎我的理解实际上是正确的。
在内部,suspend
Kotlin 中的函数接收附加Continuation
参数。它始终是最后一个参数,并且在 Kotlin 代码中是隐藏的。但是,如果我们查看生成的字节码,我们会看到registerDeviceAsync()
函数实际上接收了 2 个参数。
如果我们使用的某些工具不知道挂起函数,它将无法正确解释这个附加参数。它只会“认为”registerDeviceAsync()
有两个参数。Retrofit 在 version 中添加了对挂起函数的支持2.6.0
,所以我想如果使用带有挂起函数的旧版本,我们将得到你所面临的行为。
您只需要将 Retrofit 更新到较新的版本。
推荐阅读
- snowflake-cloud-data-platform - 根据表 B 的拆分百分比雪花拆分表 A 的整数列
- php - 将带有名称和描述的 PHP 文本框发布到 HTML/Rest IIS 服务器
- c# - StreamWriter 和 BinaryWriter 在写入字符串时是否可以互换
- xml - 如何为多值 IMPORTXML 函数添加空白行?
- c++ - Nm 在共享库中显示符号,但链接时未定义符号
- typescript - 即使没有修改任何内容,Webpack 也会在保存时出错
- c++ - (.text+0x26f): 未定义的对 `TextOutA@20' 的引用
- ios - 在没有 iPad 的情况下在 React Native 上构建 iOS iPad 应用程序
- android - 检测 Osmdroid Marker 拖放到 View
- python - heroku postgres psycopg2.errors.UndefinedTable