71

I am using Retrofit and Robospice to make API calls in my android application. All @POST methods work great, and so do @GET commands without any parameters in the URL, but I can't get any @GET calls to work with parameters on the end!

For example, if my API path was "my/api/call/" and I wanted 2 parameters "param1" and "param2" in the URL, the get call would look like:

http://www.example.com/my/api/call?param1=value1&param2=value2

so I have setup my @GET interface like so:

@GET("/my/api/call?param1={p1}&param2={p2}")
Response getMyThing(@Path("p1")
String param1, @Path("p2")
String param2);

but I get an error saying
"An exception occurred during request network execution : URL query string "/my/api/call?param1={p1}&param2={p2}" on method getMyThing may not have replaced block."

What am I doing wrong?

jsingh
  • 1,256
  • 13
  • 24
AndroidNoob
  • 2,771
  • 2
  • 40
  • 53

7 Answers7

142

You should be using this syntax:

@GET("/my/API/call")
Response getMyThing(
    @Query("param1") String param1,
    @Query("param2") String param2);

Specifying query parameters in the URL is only for when you know both the key and value and they are fixed.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • 2
    I'm getting the following error now: 11-14 10:29:48.626: E//RequestRunner.java:134(31776): 10:29:48.630 Thread-11782 An exception occurred during request network execution :null I'm definitely setting the interface in my spicemanager, not sure what could be null... – AndroidNoob Nov 14 '13 at 10:35
  • 2
    I'm not sure, sorry. I have never used RoboSpice. I'm just here for the Retrofit :) – Jake Wharton Nov 14 '13 at 12:03
  • Hi Jake, that would we perfect in the scenario where you have to construct the query params yourself, but what about if they are given to you previosly? Imagine a template response which tell you where to fetch the info from and has those params for you to set. So /my/api/call?param1={p1}&param2={p2} is not actually created by you manually, but provided to you in the response of another call. – Javier Tarazaga Apr 17 '14 at 14:50
  • 1
    @Javier Tarazaga sorry this is late but in retrofit 1 and 2 you can use a `QueryMap` - see [this example here](https://futurestud.io/blog/retrofit-2-add-multiple-query-parameter-with-querymap) – AndroidNoob Jul 05 '16 at 10:01
23

If you have a bunch of GET params, another way to pass them into your url is a HashMap.

class YourActivity extends Activity {

    private static final String BASEPATH = "http://www.example.com";

    private interface API {
        @GET("/thing")
        void getMyThing(@QueryMap Map<String, String>, new Callback<String> callback);
    }

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.your_layout);

       RestAdapter rest = new RestAdapter.Builder().setEndpoint(BASEPATH).build();
       API service      = rest.create(API.class);

       Map<String, String> params = new HashMap<String, String>();
       params.put("foo", "bar");
       params.put("baz", "qux");
       // ... as much as you need.

       service.getMyThing(params, new Callback<String>() {
           // ... do some stuff here.
       });
    }
}

The URL called will be http://www.example.com/thing/?foo=bar&baz=qux

Julio Betta
  • 2,275
  • 1
  • 25
  • 25
14

You can create a Map of params and send it like below:

Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("p1", param1);
paramsMap.put("p2", param2);

// Inside call
@GET("/my/api/call")
Response getMyThing(@QueryMap Map<String, String> paramsMap);
Ankit Bisht
  • 1,209
  • 12
  • 14
6

Do not write your Query params in GET-URL. Do it like this:

@GET("/my/api/call")
Response getMyThing(@Query("param1") String param1,
                    @Query("param2") String param2);
Christopher
  • 9,682
  • 7
  • 47
  • 76
3

with Java

@GET("/my/api/call")
Response getMyThing(@Query("p1")
String param1, @Query("p2")
String param2);

with Kotlin coroutines

 @GET("/my/api/call")
 suspend fun getSearchProduct(@Query("p1") p1: String , @Query("p2") p2: String )
eng mohamed emam
  • 549
  • 6
  • 13
0

This is the best way you can use:

@GET("api_link_put_here")
    apiResponse getMyData(
            @Query("first_param") String firstParam,
            @Query("second_param") String secondParam
    );

this will automatically work like: http://baseurl.com/api_link_put_here?first_param=firstParam&second_param=secondParam

0

Retrofit with Composable

@GET("api_link_put_here")
fun getapi_link_put_here(@Query("firstParam") firstParam: String,
                         @Query("second_param") second_param: String): 
 Call<ResponseBody>
Mike Zriel
  • 1,575
  • 1
  • 17
  • 28