java.lang.IllegalArgumentException:未找到 Retrofit 注释。(参数#4)

Pra*_*kar 1 android mvvm kotlin retrofit

我正在尝试连接到 REST API 端点

以下是我的WebService.kt

package <package_name>.data.services

import ch.iazi.paidrent.BuildConfig
import ch.iazi.paidrent.models.DefaultParameters
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Query

interface WebService {

    companion object{
         const val ENDPOINT_DEFAULT_PARAMETERS = "/appDefaultParameterValues"
    }

   @Headers("Content-Type: application/x-www-form-urlencoded")
   @GET(BuildConfig.APPVERSION + ENDPOINT_DEFAULT_PARAMETERS)
   suspend fun getDefaultParameters(
      @Query("app") app: String,
      @Query("tid") tid: Long,
      @Query("uid") uid: String
   ): Response<DefaultParameters>

}
Run Code Online (Sandbox Code Playgroud)

这是我的 Repository.kt -

package <package_name>.data

import androidx.lifecycle.LiveData
import <package_name>.data.services.ServiceBuilder
import <package_name>.data.services.WebService
import <package_name>.models.DefaultParameters
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch

object Repository{

    val API = ServiceBuilder.buildWebService(WebService::class.java)

    fun getDefaultParameters(app:String,tid:Long,uid:String):LiveData<DefaultParameters>{
        return object : LiveData<DefaultParameters>(){
            override fun onActive() {
                super.onActive()
                CoroutineScope(IO).launch {
                    val response = API.getDefaultParameters(app,tid,uid)
                    print(response)
                }
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我调用存储库中的方法时的 logcat:-

2020-03-02 15:15:48.112 13174-13251/<package_name> E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
    Process: <package_name>, PID: 13174
    java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #4)
        for method WebService.getDefaultParameters
        at retrofit2.Utils.methodError(Utils.java:52)
        at retrofit2.Utils.methodError(Utils.java:42)
        at retrofit2.Utils.parameterError(Utils.java:61)
        at retrofit2.RequestFactory$Builder.parseParameter(RequestFactory.java:311)
        at retrofit2.RequestFactory$Builder.build(RequestFactory.java:182)
        at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:65)
        at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:25)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
        at $Proxy1.getDefaultParameters(Unknown Source)
        at ch.iazi.paidrent.data.Repository$getDefaultParameters$1$onActive$1.invokeSuspend(Repository.kt:20)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)
Run Code Online (Sandbox Code Playgroud)

我已使用 @Query 注释在接口定义中注释了所有 3 个参数。日志消息显示参数 #4 。但是我的端点调用中没有 4 个参数。我在这里错过了什么吗?

Pra*_*kar 6

通过将 build.gradle 中的改造版本从 2.5.0 更新到 2.6.0 解决了这个问题。从 2.6.0 版本开始,支持 Kotlin 协程。

谢谢