在 Jetpack Compose 中,如何基于 ids 进行动态 API 端点改造?

问题描述 投票:0回答:1

我希望能够使用动态 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")

android kotlin android-jetpack-compose retrofit retrofit2
1个回答
0
投票
    interface MyApi {
    @GET("/my/endpoint/{myId}")
    suspend fun getData(@Path("myId") myId:String): MyResponse
}
© www.soinside.com 2019 - 2024. All rights reserved.