Retrofit方括号查询参数

huangapple go评论126阅读模式
英文:

Retrofit Square Brackets Query Parameters

问题

I have the following GET URL that requires multiple parameters:

https://www.games.com/game?filter[condition][path]=season&filter[condition][value]=Season1&page[limit]=1&page[offset]=0

However, I can't find a way to represent this parametrized call using Retrofit because it's using Square Brackets.

I've tried something like this, but it doesn't work, since Retrofit cannot recognize those parameters.

@Headers("Content-Type: application/json")
@GET("game")
Call getAllGames(
@Query("filter[condition][path]") String conditionFilter,
@Query("filter[condition][value]") String conditionValue,
@Query("page[limit]") String pageLimit,
@Query("page[offset]") String pageOffset
);

Any hint?

英文:

I have the following GET URL that requires multiple parameters:

https://www.games.com/game?filter[condition][path]=season&filter[condition][value]=Season1&page[limit]=1&page[offset]=0

However, I can't find a way to represent this parametrized call using Retrofit because it's using Square Brackets.

I've tried something like this, but it doesn't work, since Retrofit cannot recognize those parameters.

@Headers("Content-Type: application/json")
@GET("game")
Call<FunctionsResponse> getAllGames(
        @Query("filter[condition][path]") String conditionFilter,
        @Query("filter[condition][value]") String conditionValue,
        @Query("page[limit]") String pageLimit,
        @Query("page[offset]") String pageOffset
);

Any hint?

答案1

得分: 1

尝试这个并告诉我是否仍然出现错误:

@Headers("Content-Type: application/json")
@GET("game")
Call<FunctionsResponse> getAllGames(
        @Query("filter%5Bcondition%5D%5Bpath%5D") String conditionFilter,
        @Query("filter%5Bcondition%5D%5Bvalue%5D") String conditionValue,
        @Query("page%5Blimit%5D") String pageLimit,
        @Query("page%5Boffset%5D") String pageOffset
);

在这段代码中,方括号 [ 和 ] 被编码为 %5B 和 %5D,分别使用URL编码。Retrofit会正确地将带有方括号的查询参数进行编码,并将它们包含在请求URL中。

英文:

Try this and let me know if you still got error

@Headers(&quot;Content-Type: application/json&quot;)
@GET(&quot;game&quot;)
Call&lt;FunctionsResponse&gt; getAllGames(
        @Query(&quot;filter%5Bcondition%5D%5Bpath%5D&quot;) String conditionFilter,
        @Query(&quot;filter%5Bcondition%5D%5Bvalue%5D&quot;) String conditionValue,
        @Query(&quot;page%5Blimit%5D&quot;) String pageLimit,
        @Query(&quot;page%5Boffset%5D&quot;) String pageOffset
);

In this code, the square brackets [ and ] are encoded as %5B and %5D, respectively, using URL encoding. Retrofit will properly encode the query parameters with square brackets and include them in the request URL.

答案2

得分: 0

You can use the @QueryMap annotation instead of individual @Query annotations.

Here is how you can achieve it:

Map<String, String> queryMap = new HashMap<>();
queryMap.put("filter[condition][path]", "season");
queryMap.put("filter[condition][value]", "Season1");
queryMap.put("page[limit]", "1");
queryMap.put("page[offset]", "0");
Call<FunctionsResponse> call = yourApiService.getAllGames(queryMap);
英文:

You can use the @QueryMap annotation instead of individual @Query annotations.

Here is how you can achieve it:

Map&lt;String, String&gt; queryMap = new HashMap&lt;&gt;();
queryMap.put(&quot;filter[condition][path]&quot;, &quot;season&quot;);
queryMap.put(&quot;filter[condition][value]&quot;, &quot;Season1&quot;);
queryMap.put(&quot;page[limit]&quot;, &quot;1&quot;);
queryMap.put(&quot;page[offset]&quot;, &quot;0&quot;);
Call&lt;FunctionsResponse&gt; call = yourApiService.getAllGames(queryMap);

huangapple
  • 本文由 发表于 2023年7月6日 17:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定