首页 > 解决方案 > 改造寻找不存在的参数?

问题描述

我正在使用改造在我的简单 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 找到注释

未找到参数 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)

那么为什么要寻找第二个参数呢?我错过了什么?

标签: androidkotlinretrofit

解决方案


我对改造不是很熟悉,我在这里做一个有根据的猜测,但在评论中的简短讨论之后,似乎我的理解实际上是正确的。

在内部,suspendKotlin 中的函数接收附加Continuation参数。它始终是最后一个参数,并且在 Kotlin 代码中是隐藏的。但是,如果我们查看生成的字节码,我们会看到registerDeviceAsync()函数实际上接收了 2 个参数。

如果我们使用的某些工具不知道挂起函数,它将无法正确解释这个附加参数。它只会“认为”registerDeviceAsync()有两个参数。Retrofit 在 version 中添加了对挂起函数的支持2.6.0,所以我想如果使用带有挂起函数的旧版本,我们将得到你所面临的行为。

您只需要将 Retrofit 更新到较新的版本。


推荐阅读