retrofit 2 @path Vs @query

后端 未结 6 398
暖寄归人
暖寄归人 2021-01-29 23:25

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

相关标签:
6条回答
  • 2021-01-29 23:49

    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.

    0 讨论(0)
  • 2021-01-29 23:59

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

    0 讨论(0)
  • 2021-01-30 00:01

    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}
    
    0 讨论(0)
  • 2021-01-30 00:08

    @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
    0 讨论(0)
  • 2021-01-30 00:11

    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)
    
    0 讨论(0)
  • 2021-01-30 00:13

    @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);
    
    0 讨论(0)
提交回复
热议问题