我希望能够使用动态 API 端点 URL 来获取特定数据。我已经能够在我的
@GET("endpoint")
中使用固定的 URL,这是有效的,但我不知道如何动态使用它。
我的班级设置如下:
class WebService {
private lateinit var api: MyApi
init {
val retrofit = Retrofit.Builder()
.baseUrl("https://myUrl.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
api = retrofit.create(MyApi::class.java)
}
suspend fun getData(): MyResponse {
return api.getData()
}
interface MyApi {
@GET("/my/endpoint/1")
suspend fun getData(): MyResponse
}
}
我怎样才能重新格式化它以便我可以制作它
@GET("/my/endpoint/MY_ID")
?
interface MyApi {
@GET("/my/endpoint/{myId}")
suspend fun getData(@Path("myId") myId:String): MyResponse
}