在经过改造的情况下,在URL路径中添加参数

时间:2019-02-28 06:19:03

标签: kotlin interface retrofit okhttp

我创建了如下所示的API:

interface listProd {
  @GET("v1/{type}")
  fun getProduct(
      @Path("type") type: String): Observable<Response>
}

现在我需要添加一个参数以使其如下所示:

interface listProd {
  @GET("v1/{type}?preview=true")
  fun getProduct(
      @Path("type") type: String): Observable<Response>
}

但是我不想一直都有它。目的是要了解如何在布尔条件下添加?preview=true,我不想有多个接口,但是想添加或不添加一个聪明的方法。

条件可以是getProduct中的布尔传递,这会触发是否附加?preview=true

任何想法 谢谢

1 个答案:

答案 0 :(得分:0)

您应该能够为此使用可选的查询参数:

@GET("v1/{type}")
fun getProduct(
    @Path("type") type: String,
    @Query("preview") preview: Boolean?
): Observable<Response>

需要注意的是,传入truefalse将包含该参数,而null则将其完全省略。