I am new to retrofit 2 library.I read several articles to get started as a beginner and I managed to fetch XML data from my RESTful API without specifying parameters.In my metho
Consider this is the url:
www.app.net/api/searchtypes/862189/filters?Type=6&SearchText=School
Now this is the call:
@GET("/api/searchtypes/{Id}/filters")
Call<FilterResponse> getFilterList(
@Path("Id") long customerId,
@Query("Type") String responseType,
@Query("SearchText") String searchText
);
So we have:
www.app.net/api/searchtypes/{Path}/filters?Type={Query}&SearchText={Query}
Things that come after the ? are usually queries.
@Path is used when you have url which has '/' dynamic value after a backword slash.Example "http://google.com/index.html/userid. So in this url /userid is dynamic so to access this url your request should be @Get("index.html/{userid}") Calldata(@Path("userid")int id);
@Query is used when you have a url which has '?' dynamic value after a question mark.Example "http://google.com/index.html?userid.So in this url ? userid is dynamic so to access this url your request should be @Get("index.html") Calldata(@Query("userid")int id);
Query is use for URL parameters and with @Query("password") the URL should be :
user/john?password=****
Path is use to replace item defined in your path, like
user/{username}
@Query
@Path
For example:
@GET("/user/{username}?type={admin}")
Here username
is the path
variable, and type
is the query variable
@GET("/user/{username}?type={admin}")
void getUserOuth(@Path("username") String username, @Query("type") String type)
@Path annotation use for ordering parameters as your own way. And defined the order in url.
@GET("user/{username}/{password}")
Call<List<UserCredentail>> getUserOuth(@Path("username") String username, @Path("password") String password);
@Query annotation auto order of parameters and added with url including "?" symbol.
@GET("user")
Call<List<UserCredentail>> getUserOuth(@Query("username") String username, @Query("password") String password);