@Path、@Query、@Post、Body等总结

http://192.168.43.173/api/trades
//简单的get请求(没有参数)
 @Headers("Cache-Control: no-cache") 
 @GET("trades")
 Call<TradesBean> getItem();
 
http://192.168.43.173/api/trades/{userId}
//简单的get请求(URL中带有参数)
  @GET("News/{userId}")
  Call<TradesBean> getItem(@Path("userId") String userId);
 
//简单的get请求(URL中带有两个参数)
  @GET("News/{userId}")
  Call<TradesBean> getItem(@Path("userId") String userId,@Path("type") String type);
 
http://192.168.43.173/api/trades?userId={用户id}
//参数在url问号之后
 @GET("trades")
 Call<TradesBean> getItem(@Query("userId") String userId);
 
http://192.168.43.173/api/trades?userId={用户id}&type={类型}
 @GET("trades")
 Call<TradesBean> getItem(@QueryMap Map<String, String> map);
 
 @GET("trades")
 Call<TradesBean> getItem(
                @Query("userId") String userId,
                @QueryMap Map<String, String> map);
 
POST
http://192.168.43.173/api/trades/{userId}
//需要补全URL,post的数据只有一条reason
 @FormUrlEncoded
 @POST("trades/{userId}")
 Call<TradesBean> postResult(
         @Path("userId") String userId,
         @Field("reason") String reason;
 
http://192.168.43.173/api/trades/{userId}?token={token}
//需要补全URL,问号后需要加token,post的数据只有一条reason
 @FormUrlEncoded
 @POST("trades/{userId}")
 Call<TradesBean> postResult(
         @Path("userId") String userId,
         @Query("token") String token,
         @Field("reason") String reason;
 
//post一个对象
 @POST("trades/{userId}")
 Call<TradesBean> postResult(
         @Path("userId") String userId,
         @Query("token") String token,
         @Body TradesBean bean;
 
//用不同注解post一个实体
 @POST("trades/{userId}")
 Call<TradesBean> postResult(
         @Part("entity") TradesBean bean;
 
PUT
//put一个实体
 @PUT("trade/carInfo/{pid}")
 Call<TradesBean> putInfo(
             @Path("pid") Int pid,
             @Body CarInfoBean carInfoBean;)
 
DELETE
http://192.168.43.173/api/trades/{userId}
//补全url
 @DELETE("trades/{userId}")
 Call<TradesBean> deleteInfo(
         @Path("userId") String userId;  
 
http://192.168.43.173/api/trades/{userId}?token={token}
//补全url并且后面还token
 @DELETE("trades/{userId}")
 Call<TradesBean> deleteInfo(
         @Path("userId") String userId,
         @Query("token") String token;)  

 @HTTP(method = "DELETE", path = "api/fhs/v1/fhs/{Id}", hasBody = true)
 Observable<String> deleteFhs(@Path("Id") String Id);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容