改造动态网址

时间:2018-06-12 19:44:59

标签: android retrofit2

如何注释改造方法以通过"& page = 1"和"& per_page"以下链接中的参数:https://api.github.com/search/repositories?q=tetris&page=1&per_page=10 我希望能够在运行时更改这些参数。  我想出了如何注释查询参数"俄罗斯方块",但我无法找到"& page = 1"和"& per_page"参数。这是我的改造界面:

String BASE_URL = "https://api.github.com/";

@Headers("User-Agent: useragent")
@GET("search/repositories")
Call<GitHubRepo> searchRepos(@Query("q") String searchParam);

2 个答案:

答案 0 :(得分:4)

这些也是查询参数。

网址中的第一个查询参数始终使用&#39;?&#39;添加,并且所有后续查询参数都使用&#39;&amp;&#39;添加。这是网址的标准,不仅适用于Android,也适用于所有网址。

Retrofit会正确使用&#39;?&#39;和&#39;&amp;&#39;在适合您的地方,只需添加您想要的所有查询参数,就像您在当前设置中添加它们一样,它应该可以正常工作。

答案 1 :(得分:2)

@GET("search/repositories")
Call<GitHubRepo> searchRepos(
    @Query("page") Integer page),
    @Query("per_page") Integer perPage
)