Java 改造2@path Vs@query
我是第2库的新手。作为初学者,我阅读了几篇文章开始学习,并且在不指定参数的情况下从RESTful API获取XML数据。下面是生成XML资源的方法Java 改造2@path Vs@query,java,android,rest,retrofit2,Java,Android,Rest,Retrofit2,我是第2库的新手。作为初学者,我阅读了几篇文章开始学习,并且在不指定参数的情况下从RESTful API获取XML数据。下面是生成XML资源的方法 @GET @Path("/foods") @Produces(MediaType.APPLICATION_XML) public List<FoodPyramid> getFoodPyramid() { Session session = HibernateUtil.getSessionFactory().openSession(
@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;
}
@GET
@路径(“/foods”)
@生成(MediaType.APPLICATION\u XML)
公共列表getFoodPyramid(){
Session Session=HibernateUtil.getSessionFactory().openSession();
trans=session.beginTransaction();
List foodList=session.createQuery(“来自FoodPyramid”).List();
试一试{
trans.commit();
session.close();
}捕获(例外e){
session.close();
系统错误println(“食物金字塔提取”+e);
}
System.err.println(“食品模式中的Am……”);
返回食物列表;
}
现在,当我尝试在接口中传递参数时
@GET("user/{username}/{password}")
Call<List<UserCredentail>> getUserOuth(@Query("username") String username, @Query("password") String password);
@GET(“用户/{username}/{password}”)
调用getUserOuth(@Query(“username”)字符串username,@Query(“password”)字符串password);
运行失败,客户端未接收任何数据。我花了一周的时间试图通过使用非参数调用获取资源来修复它;
因此尝试将其更改为:
@GET("user/{username}/{password}")
Call<List<UserCredentail>> getUserOuth(@Path("username") String username, @Path("password") String password);
@GET(“用户/{username}/{password}”)
调用getUserOuth(@Path(“username”)字符串username,@Path(“password”)字符串password);
而且效果很好。所以我的问题是:什么时候我需要在改型2中使用
@Query
和@Path
注释?考虑以下url:
www.app.net/api/searchtypes/862189/filters?Type=6&SearchText=School
现在是电话:
@GET("/api/searchtypes/{Id}/filters")
Call<FilterResponse> getFilterList(
@Path("Id") long customerId,
@Query("Type") String responseType,
@Query("SearchText") String searchText
);
?后面的内容通常是查询。查询用于URL参数,使用@Query(“密码”)时,URL应为:
user/john?password=****
路径用于替换路径中定义的项,如
user/{username}
例如:
@GET("/user/{username}?type={admin}")
这里,username
是path
变量,type
是查询变量
@GET("/user/{username}?type={admin}")
void getUserOuth(@Path("username") String username, @Query("type") String type)
@当您的url在反斜杠后有“/”动态值时,将使用Path。例如”。因此,在该url中/userid是动态的,因此要访问该url,您的请求应该是 @获取(“index.html/{userid}”) Calldata(@Path(“userid”)int-id) @当您有一个在问号后面有“?”动态值的url时,将使用查询?userid是动态的,因此要访问此url,您的请求应该是 @获取(“index.html”)
Calldata(@Query(“userid”)int-id) @路径注释用于按自己的方式对参数进行排序。并在url中定义了顺序
@GET("user/{username}/{password}")
Call<List<UserCredentail>> getUserOuth(@Path("username") String username, @Path("password") String password);
@GET(“用户/{username}/{password}”)
调用getUserOuth(@Path(“username”)字符串username,@Path(“password”)字符串password);
@查询注释参数的自动顺序,并添加包含“?”符号的url
@GET(“用户”)
调用getUserOuth(@Query(“username”)字符串username,@Query(“password”)字符串password);
@Query
- 此注释表示要随网络请求一起发送的任何查询键值对
@Path
- 此注释表示传递的参数将在端点路径中交换
- Kotlin回答
例如,使用帖子id从列表中获取特定帖子:
@GET(“Posts/{post\u id}”)
suspend fun getPost(@Path(“post_id”)postId:String):响应
@Paul让我也来试验一下。ThanksDoesn它不需要是type而不是admin作为@Query参数吗?As:void getUserOuth(@Path(“username”)String username,@Query(“type”)String type)
这应该是正确的答案,谢谢你的启发也帮了我很多!查询和路径之间存在差异,那么此url中的&mean是什么?在url type={query}中,但在@query(“type”)中,sting responsetype,那么如何在这个url中插入=sign?我对这个问题感到困惑在我挣扎了一整天之后,你救了我的命
@GET("user/{username}/{password}")
Call<List<UserCredentail>> getUserOuth(@Path("username") String username, @Path("password") String password);
@GET("user")
Call<List<UserCredentail>> getUserOuth(@Query("username") String username, @Query("password") String password);
@GET("Posts/{post_id}")
suspend fun getPost(@Path("post_id") postId: String): Response<Post>