【问题标题】:Can I send parameter to the Interface in kotlin?我可以将参数发送到 kotlin 中的接口吗?
【发布时间】:2021-02-16 14:34:34
【问题描述】:

我在改造 API 请求中使用 @Get 接口,而不是为每个调用创建 @Get 函数,我想将参数发送到 @Get 函数,我该怎么做?像下面的例子

接口 SomeApiService(catId: String) {

@GET(EN_PRODUCTLIST+catId)
fun getProductByCatId():
        Deferred<SomeProduct>

}

对象 SomeApi { val retrofitService : SomeApiService bylazy { retrofit.create(SomeApiService::class.java) } }

当我调用 SomeApi(catId).getProductByCatId() 时,它将通过 catId 返回该特定产品。 该怎么做?

【问题讨论】:

  • 不,接口不是这样工作的
  • 我如何从外部向@Get(productAtId) 发送请求?你知道我该怎么做吗?
  • 为什么不在函数中发送?
  • 它会在@Get 中起作用吗?
  • 你的意思是像:@GET(EN_PRODUCTLIST) fun getProductByCatId(catId: String): Deferred

标签: android api kotlin interface retrofit


【解决方案1】:

你必须这样做

@GET("EN_PRODUCTLIST{catId}")
fun getProductByCatId(@Path(catId) catId:String):Deferred<SomeProduct>

@Path 将参数放入请求的url路径中。

请再次阅读文档https://square.github.io/retrofit/

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多