M D
M D

Reputation: 47807

How to use @Query in URL in Retrofit?

Hi all i call below URL using Retrofit

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

and for that i created 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);

When i do this it always add key at the end of the URL(Output below).

I added @Query("key") as Query Parameter becoz it's dynamic.

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

and that's the wrong. I got HTTP 400. Also here (( and )) converted into %28%28 and %29%29

Please help me how to make

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

in Retrofit. I want it add @Query parameter in between URL. not at the end of the URL

Upvotes: 0

Views: 2837

Answers (2)

learner
learner

Reputation: 3110

Don't put query parameter inside the URL only the path parameter you can add

@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

While sending request your URL form will like below

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

Upvotes: 1

Selvin
Selvin

Reputation: 6797

obviously instead

@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);

you should use

@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);

the chanege is /me& to /me? ... (and second is to use access_token as Query param too, not as a Path)

edit more explanations:

After parsing url which looks like (me&)

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

the path is

me&blablalbal=blalbla&blabla=blabla

and there is no query paramas at all ... so adding params end with adding ?param=value at the end

but with (me?)

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

the path is

me

and there are already some query parameters ... so addin new end with adding &param=value at the end :)

Upvotes: 0

Related Questions