项目中使用 Rxjava 进行网络请求,请求的方式有 get 请求和 post 请求,而请求中 有时候 需要 请求头 head,有时候需要 请求 json 串,琳琳种种,梳理下思路:
#1. get 请求
## 无参数-get
```
@GET("login/sys")
ObservablegetTKUrl();
```
## 有参数-get ,用 Query 查询字段
```
@GET("customer/login")
Observable<PolicRecordBean> getPolicyRecord(
@Query("id") String id,
@Query("token") String token );
```
多个参数请求
```
@GET("News")
Call<NewsBean> getItem(
@Query("newsId") String newsId,
@QueryMap Map<String, String> map);
```
#2. post请求 -- 大部分为有参,增加安全性
##2.1 post 简单请求
```
@POST("preservation/login")
Observable<HeadBanner> getHeadBanner(
@Query("token") String token
);
```
##2.2 post 普通请求 Field 与 FormUrlEncoded 联用
```
@FormUrlEncoded
@POST("preservation/login")
Observable<Loan> loan(
@Field("param") String param,
@Field("sign") String sign,
@Query("token") String token
);
```
## 2.3 多参数-post
```
@FormUrlEncoded
@POST("cos/getPubSignParams")
Observable<TencentCloudParams> getTencentCloudParams(
@FieldMap Map<String, String> params,
@Query("token") String token
);
```
## 2.4 补全url
```
@FormUrlEncoded
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Query("access_token") String access_token,
@Field("reason") String reason);
```
## 2.5 body 相当于多个@Field,以对象的形式提交
```
@POST("cos/getSignParamsNew&