英文:
Retrofit Square Brackets Query Parameters
问题
I have the following GET URL that requires multiple parameters:
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
@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("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
);
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<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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论