如何在Retrofit中使用URL中的@Query?

时间:2015-12-14 12:02:07

标签: android retrofit

我使用Retrofit

网址下面打电话
https://api.stackexchange.com/2.2/me?key=gQJsL7krOvbXkJ0NEI((&site=stackoverflow&order=desc&sort=reputation&access_token=HM*22z8nkaaoyjA8))&filter=default

为此我创建了Interface RestInterface

 //get UserId
 @GET("/me&site=stackoverflow&order=desc&sort=reputation&access_token={access_token}&filter=default")
 void getUserId(@Query("key") String apikey,@Path("access_token") String access_token,Callback<UserShortInfo> cb);

当我这样做时,它总是在URL(Output below)的末尾添加密钥。

我添加了 @Query(&#34;键&#34;),因为Query Parameter让它充满活力。

 http://api.stackexchange.com/2.2/me&site=stackoverflow&order=desc&sort=reputation&access_token=p0j3dWLIcYQCycUHPdrA%29%29&filter=default?key=gQJsL7krOvbXkJ0NEI%28%28 

那是错的。我得到了HTTP 400。此处(())转换为%28%28%29%29

请帮我怎样制作

 https://api.stackexchange.com/2.2/me?key=gQJsL7krOvbXkJ0NEI((&site=stackoverflow&order=desc&sort=reputation&access_token=HM*22z8nkaaoyjA8))&filter=default
Retrofit中的

。我希望它在@Query之间添加URL参数。不在URL

的末尾

2 个答案:

答案 0 :(得分:1)

不要将查询参数放在URL中,只能添加路径参数

@GET("/me?site=stackoverflow&order=desc&sort=reputation&filter=default)
void getUserId(@Query("key") String apikey,@Query("access_token") String access_token,Callback<UserShortInfo> cb);

@Query("access_token") --> given key and value will come query URL

发送请求时,您的网址表格如下所示

/me?key=?site=stackoverflow&order=desc&sort=reputation&filter=default&"your_value"&access_token="your_value"

答案 1 :(得分:0)

显然是

@GET("/me&site=stackoverflow&order=desc&sort=reputation&access_token={access_token}&filter=default")
void getUserId(@Query("key") String apikey,@Path("access_token") String access_token,Callback<UserShortInfo> cb);

你应该使用

@GET("/me?site=stackoverflow&order=desc&sort=reputation&filter=default")
void getUserId(@Query("key") String apikey,@Query("access_token") String access_token,Callback<UserShortInfo> cb);

chanege为/me&/me? ...(其次是使用access_token作为Query param,而不是Path)< / p>

修改更多解释:

解析看起来像(me&

的网址
scheme:host/me&blablalbal=blalbla&blabla=blabla

路径是

me&blablalbal=blalbla&blabla=blabla

并且根本没有查询参数...所以添加参数结束时最后添加?param=value

但有(me?

scheme:host/me?blablalbal=blalbla&blabla=blabla

路径是

me

并且已经存在一些查询参数...所以在最后添加&param=value时添加新的结尾:)