【问题标题】:How to pass custom query parameters in Retrofit?如何在 Retrofit 中传递自定义查询参数?
【发布时间】:2019-12-06 08:41:18
【问题描述】:

尝试实现与 TestRail API(http://docs.gurock.com/testrail-api2/accessing) 交互的自定义​​解决方案,我有点陷入以下情况:

Api 调用是这样进行的:/index.php?/api/v2/get_case/1,这意味着在任何东西之后“?”是一个查询字符串参数.有没有办法通过 Retrofit 对其进行参数化?

如果我这样做:

@GET("index.php?/api/v2/get_case/{id}")
Call<TestCase> getTestCase(@Query("id") int id);

我得到了这个例外:

java.lang.IllegalArgumentException: URL query string "/api/v2/get_case/{id}" must not have replace block. For dynamic query parameters use @Query.

知道了...但是如何继续使用 Retrofit?

【问题讨论】:

    标签: retrofit retrofit2 okhttp testrail


    【解决方案1】:

    通过拦截器解决了这个问题

    Request currentRequest = chain.request();
    String finalURL = currentRequest.url().toString().replace("index.php/", "index.php?/");
    
    Request.Builder request = currentRequest.newBuilder()
            .addHeader("Authorization", authToken)
            .addHeader("Content-Type", ContentType.JSON.toString())
            .url(finalURL);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 2015-08-26
      • 2021-06-07
      相关资源
      最近更新 更多