文章目录
- 一、Retrift简介
- 二、Retrift使用介绍
- 三、注解
- 1、GET
- 2、POST
- 3、PUT
- 4、DELETE
 
一、Retrift简介
retrift官网
1、是一个基于okhttp的网络请求框架
 2、通过注解配置网络请求参数
 3、图片链接和图片上传
 4、支持同步和异步网络请求
 5、支持多种数据的解析,提供对Rxjava的支持
 6、可拓展性好,高度封装,简洁易用
二、Retrift使用介绍
1、添加Retrofit库的依赖
 2、创建接收服务器返回数据的类
 3、创建用于描述网络请求的接口
 4、创建 Retrofit 实例
 5、创建 网络请求接口实例 并 配置网络请求参数
 6、发送网络请求(异步 / 同步)
 7、处理数据
三、注解
参考:https://zhuanlan.zhihu.com/p/141592512?from_voters_page=true

1、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);
2、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;
3、PUT
//put一个实体
 @PUT("trade/carInfo/{pid}")
 Call<TradesBean> putInfo(
             @Path("pid") Int pid,
             @Body CarInfoBean carInfoBean;)
4、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;)  



















