Java 改造 2 @path 与 @query

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37698501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:34:46  来源:igfitidea点击:

retrofit 2 @path Vs @query

javaandroidrestretrofit2

提问by Mwesigye John Bosco

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 that generated the XML resource is below.

我是改造 2 库的新手。我阅读了几篇文章作为初学者入门,我设法从我的 RESTful API 获取 XML 数据而不指定参数。在我生成 XML 资源的方法中,如下所示。

@GET
@Path("/foods")
@Produces(MediaType.APPLICATION_XML)
public List<FoodPyramid> getFoodPyramid() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    trans = session.beginTransaction();
    List<FoodPyramid> foodList = session.createQuery("from FoodPyramid").list();
    try {
        trans.commit();
        session.close();
    } catch (Exception e) {
        session.close();
        System.err.println("Food Pyramid fetch " + e);
    }
    System.err.println("Am in the food modal. . . . . . . .");
    return foodList;
}

Now when I tried to pass parameter in the interface

现在当我尝试在界面中传递参数时

@GET("user/{username}/{password}")
Call<List<UserCredentail>> getUserOuth(@Query("username") String username, @Query("password") String password);  

It failed to run,no data was receive by a client . It took me a week trying to fix it though by using a non parameter call fetched the resources; So tried to change it to:

它无法运行,客户端未收到任何数据。我花了一周的时间试图通过使用非参数调用获取资源来修复它;所以尝试将其更改为:

@GET("user/{username}/{password}")
Call<List<UserCredentail>> getUserOuth(@Path("username") String username, @Path("password") String password);  

and it worked fine. So My question is: When do I need to use @Queryand @PathAnnotation in retrofit 2?

它工作正常。所以我的问题是:我什么时候需要在改造 2 中使用@Query@Path注释?

回答by Nongthonbam Tonthoi

Consider this is the url:

考虑这是网址:

www.app.net/api/searchtypes/862189/filters?Type=6&SearchText=School

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.

之后的事情通常是查询。

回答by Johann67

Query is use for URL parameters and with @Query("password") the URL should be :

查询用于 URL 参数,使用 @Query("password") 时,URL 应该是:

user/john?password=****

Path is use to replace item defined in your path, like

路径用于替换路径中定义的项目,例如

user/{username}

回答by Nikson Kanti Paul

For example:

例如:

@GET("/user/{username}?type={admin}")

Here usernameis the pathvariable, and typeis the query variable

这里usernamepath变量,type是查询变量

@GET("/user/{username}?type={admin}")
void getUserOuth(@Path("username") String username, @Query("type") String type)

回答by Malti Devnani

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

@Path 用于当您的 url 在反向斜杠后具有 '/' 动态值。例如“ http://google.com/index.html/userid。所以在这个 url /userid 是动态的,所以要访问这个 url 你的请求应该是@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.Soin this url ? userid is dynamic so to access this url your request should be @Get("index.html") Calldata(@Query("userid")int id);

@Query 当你有一个带有 '?' 的 url 时使用。问号后的动态值。例如“ http://google.com/index.html?userid.So在这个 url 中?userid 是动态的所以访问这个 url 你的请求应该是 @Get("index.html") Calldata (@Query("userid")int id);

回答by Majedur Rahaman

@Path annotation use for ordering parameters as your own way. And defined the order in url.

@Path 注释用于按照您自己的方式对参数进行排序。并在 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.

@Query 注释自动参数顺序并添加了包含“?”的 url 象征。

   @GET("user")
    Call<List<UserCredentail>> getUserOuth(@Query("username") String username, @Query("password") String password);

回答by Mina

@Query

@Query

  • This annotation represents any query key value pair to be sent along with the network request
  • 这个注解代表任何与网络请求一起发送的查询键值对

@Path

@Path

  • This annotation implies that the passed parameter will be swapped in the endpoint path
  • 这个注解意味着传递的参数将在端点路径中交换