102

I am using a query parameters to set the values needed by the Google Maps API.

The issue is I do not need the & sign for the first query parameter.

@GET("/maps/api/geocode/json?")
    Call<JsonObject> getLocationInfo(@Query("address") String zipCode,
                                             @Query("sensor") boolean sensor,
                                             @Query("client") String client,
                                             @Query("signature") String signature);

Retrofit generates:

&address=90210&sensor=false&client=gme-client&signature=signkey

which causes the call the fail when I need it to be

address=90210&sensor=false&client=gme-client&signature=signkey

How do I fix this?

Alan
  • 9,331
  • 14
  • 52
  • 97

3 Answers3

144

If you specify @GET("foobar?a=5"), then any @Query("b") must be appended using &, producing something like foobar?a=5&b=7.

If you specify @GET("foobar"), then the first @Query must be appended using ?, producing something like foobar?b=7.

That's how Retrofit works.

When you specify @GET("foobar?"), Retrofit thinks you already gave some query parameter, and appends more query parameters using &.

Remove the ?, and you will get the desired result.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • this works. Do you happen to have any good resources on how urls are structured and all types of parameters? I am not sure what a good keyword to search for. – Alan Apr 19 '16 at 22:15
  • 1
    Search for `URL`, and you'll find https://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax – Andreas Apr 19 '16 at 22:16
  • @Arbaz.in Then I suggest you click the "Ask Question" button in the upper right corner. – Andreas Sep 04 '19 at 15:50
91

I am new to retrofit and I am enjoying it. So here is a simple way to understand it for those that might want to query with more than one query: The ? and & are automatically added for you.

Interface:

 public interface IService {

      String BASE_URL = "https://api.test.com/";
      String API_KEY = "SFSDF24242353434";

      @GET("Search") //i.e https://api.test.com/Search?
      Call<Products> getProducts(@Query("one") String one, @Query("two") String two,    
                                @Query("key") String key)
}

It will be called this way. Considering you did the rest of the code already.

  Call<Results> call = service.productList("Whatever", "here", IService.API_KEY);

For example, when a query is returned, it will look like this.

//-> https://api.test.com/Search?one=Whatever&two=here&key=SFSDF24242353434 

Link to full project: Please star etc: https://github.com/Cosmos-it/ILoveZappos

If you found this useful, don't forget to star it please. :)

Magellan
  • 1,224
  • 9
  • 16
  • How i can see/log full query string with parametres for requests like this? – Evgeny Fedin May 18 '19 at 08:18
  • I am not quite sure how to answer that question. Are you trying to log a method results that has the eg. getProducts so you can see the log? Because you can do that many ways. I don't I have that implemented but here is a project that I have implemented Retrofit awhile back. FYI, not reviewed. https://github.com/Cosmos-it/ILoveZappos – Magellan May 19 '19 at 21:24
  • 1
    thank you for just saving me hours over hours! from your github project, I just relized that if I want to add params to my call, I'll need to change the name of my @GET method in my interface to the name of the method in the api I'm trying to use. – Hila Grossbard Oct 27 '21 at 09:01
  • @HilaGrossbard, you are welcome. I am glad you found this helpful. – Magellan Jul 02 '22 at 08:21
21
 public interface IService { 

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

  @GET("Login") //i.e https://api.demo.com/Search? 
  Call<Products> getUserDetails(@Query("email") String emailID, @Query("password") String password)

} 

It will be called this way. Considering you did the rest of the code already.

Call<Results> call = service.getUserDetails("abc@gmail.com", "Password@123");

For example when a query is returned, it will look like this.

https://api.demo.com/Login?email=abc@gmail.com&password=Password@123
Bartek Pacia
  • 1,085
  • 3
  • 15
  • 37
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53