有没有办法在 retrofitApi 的@get 中使用 var?

Is there a way to use a var in @get from the retrofitApi?

我有这个代码:

package com.example.finalv2.networking

import retrofit2.Call
import retrofit2.http.GET
import com.example.finalv2.it1



private var argument: String = it1
interface RetrofitAPI {
    @GET(argument)
    fun getInformations(): Call<ArrayList<Information>>

}

我想使用我的 var 参数,但 retrofit 需要一个 const。怎么办?

注释值是静态解析的,这意味着它们必须是常量。您可以使用函数参数的 @Url 注释传递变量参数 url。

interface RetrofitAPI {
    @GET
    fun getInformations(@Url url: String): Call<ArrayList<Information>>
}

您可以通过在函数调用中添加@Path 注释参数来实现,如下所示:

@GET("/image/{argument}")
fun getInformations(@Path("argument") argument: String): Call<ArrayList<Information>>

例如,如果函数被调用为:

getInformations("abc")

最终 URL 将向其发送请求

BASE_URL +"/image/abc"

要了解更多信息,请查看此官方文档网页, https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Path.html