Skip to content Skip to sidebar Skip to footer

Retrofit 2 @path Vs @query

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 method t

Solution 1:

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.

Solution 2:

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)

Solution 3:

@Query

  • This annotation represents any query key value pair to be sent along with the network request

@Path

  • This annotation implies that the passed parameter will be swapped in the endpoint path

Solution 4:

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

Solution 5:

Kotlin Answer

For example, geting specific post from list with post id:

@GET("Posts/{post_id}")suspendfungetPost(@Path("post_id") postId: String): Response<Post>

Note: In this example, Post is my data model class.

Post a Comment for "Retrofit 2 @path Vs @query"