Retrofit网络请求参数注解,@Path、@Query、@Post、Body等总结

原文地址:https://blog.csdn.net/guohaosir/article/details/78942485?utm_source=copy

 

Retrofit网络请求参数注解,@Path、@Query、@Post、Body等总结
具体用法参照 Retrofit官网
Retrofit简介:
是一个基于okhttp的网络请求框架
通过注解配置网络请求参数
图片链接和图片上传
支持同步和异步网络请求
支持多种数据的解析,提供对Rxjava的支持
可拓展性好,高度封装,简洁易用
Retrofit使用介绍:
使用 Retrofit 的步骤共有7个:
- 添加Retrofit库的依赖 
- 创建接收服务器返回数据的类 
- 创建用于描述网络请求的接口 
- 创建 Retrofit 实例 
- 创建 网络请求接口实例 并 配置网络请求参数 
- 发送网络请求(异步 / 同步) 
- 处理数据

 

GET
http://192.168.43.173/api/trades
//简单的get请求(没有参数)
 @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;)  


 
个人总结
Path是网址中的参数,例如:trades/{userId}
Query是问号后面的参数,例如:trades/{userId}?token={token}
QueryMap 相当于多个@Query
Field用于Post请求,提交单个数据,然后要加@FormUrlEncoded
Body相当于多个@Field,以对象的方式提交
@Streaming:用于下载大文件
@Header,@Headers、加请求头
 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值