Mcbaloo
Mcbaloo

Reputation: 149

Add query parameter to Retrofit get in Android Studio

I'm trying to use retrofit for get records from my API and it works fine when i do something like this.

public interface materialAPI {

@GET("/mlearningServices/Course")
public void getMaterials(Callback<List<materialClass>> response); }   public void getMaterials()
 {

     RestAdapter adapter = new RestAdapter.Builder().setEndpoint(Root_Url).build();
     Log.i(TAG , "hERE IS THE LINK"+adapter.toString());
     materialAPI api = adapter.create(materialAPI.class);
     api.getMaterials(new Callback <List<materialClass>>() {
         @Override
         public void success(List<materialClass> list, Response response) {

             materials = list;
             showList();
             customAdapter customAdapter = new customAdapter();
             listView.setAdapter(customAdapter);
         }

         @Override
         public void failure(RetrofitError error) {

         }
     });

 }

The above code works fine and i can get all my materials but what i want to achieve next is get material with any id . When a user selects a paticular material, i want to pass the id into the get url so i can get the records

meaning i have to do something like this

@GET("/mlearningServices/Course/{myId}")

..

How to i add myId to the callback method. This is my first time of using retrofit

Upvotes: 0

Views: 653

Answers (3)

binary
binary

Reputation: 1414

Use the @Path annotation

@POST("/mlearningServices/Course/{myId}")
public void getMaterials(@Path("myId") String id, Callback<Response> response);

References:

Upvotes: 1

Solution:

You can use this to pass your id, Use the @Path annotation

@GET("/mlearningServices/Course/{myId}")
Call<materialClass> getMaterials(@Path("myId") String id);

@Path is some data that you wish to provide it to GET method before Question Mark ("?") and @Query("..") is some data you wish to provide after "?"

Hope you have understood.

Upvotes: 0

HaroldH
HaroldH

Reputation: 533

That you are asking about is called a path variable. To set one, you must rewrite your method signature as this:

public void getMaterials(@Path("myId") String id,  Callback<List<materialClass>> response);

This way, the variable defined as /path/to/your/endpoint/{nameOfPathVariable} will be injected into that String parameter passed to the method. You could also define it as an Integer, and retrofit will try to cast it accordingly.

Upvotes: 1

Related Questions