Retrofit Get Post请求注解

963 阅读2分钟

1.Query QueryMap

Query常用在Get请求,Query注解的参数会拼接在url后面,例如url?id=1&type=A

注意:null值会被忽略;

//定义
@GET("url")
Observable<ResponseBean> getNews(@Query("id") String id,@Query("type") String type);

//调用
getNews("1","A")


QueryMap常用在Get请求,QueryMap注解的参数也同样会拼接在url后面,例如url?id=1&type=A

//定义
@GET("url")
Observable<ResponseBean> getNews(@QueryMap HashMap<String,String> map);

//调用
HashMap<String,String> map = new HashMap<String,String>();
map.put("id","1");
map.put("type","A");

Query QueryMap两者不同点:前者只能注解单个参数,后者可以注解多个参数;

2.Filed FiledMap FormUrlEncoded

跟Query QueryMap类似,只不过是用在Post请求上。Filed注解单个参数,FiledMap注解多个。 FormUrlEncoded注解是跟Post请求一起使用的,表示以表单形式提交参数(例如id=1&type=A)。如果没有该注解会报错。

//定义
@FormUrlEncoded
@POST("url")
Observable<ResponseBean> getNews(@Filed("id") String id);

//调用
getNews("1")

//定义
@FormUrlEncoded
@POST("url")
Observable<ResponseBean> getNews(@FiledMap HashMap<String,String> map);

//调用
HashMap<String,String> map = new HashMap<String,String>();
map.put("id","1");
map.put("type","A");
getNews(map)

3.Path

同样是用在Get请求的注解,Path注解的参数会替换url的占位符。

//定义
@GET("url/{id}")
Observable<ResponseBean> getNews(@Path("id") String id);

//调用
getNews("1")

正确的url是:url/1

4.Body

Body也是用在post请求,不同于Filed、FiledMap用表单形式提交参数,Body表示请求参数以json形式是放在请求体中提交。

下面的request参数会被序列化放在请求体中发到服务端。注意这里不需要使用@FormUrlEncoded

//定义
@POST("url")
Observable<ResponseBean> getNews(@Body Request request);

//定义Request
class Request {
    String id;
}


Request request = new Requst();
request.id = "1";
getNews(request)

再来看下HTTP有意思的属性Content-Type

5.Content-Type

属性放在请求头(Head),用来描述请求体数据的编码格式,大概有以下4种:

application/x-www-form-urlencoded
application/json
text/xml
multipart/form-data
  • application/x-www-form-urlencoded

这是最常见Post请求提交数据的方式,以表单形式提交数据。对应注解Filed FiledMap FormUrlEncoded。

//定义
@FormUrlEncoded
@POST("http://www.hao123.com/")
Observable<ResponseBean> getInfo(@Filed("title") String title,@Filed("title") String title);

//调用
getInfo("test",30)

//正在发起的报文
POST http://www.hao123.com/ HTTP/1.1   -------------------请求行
Content-Type: application/x-www-form-urlencoded;charset=utf-8 -------------------请求头
name=test&age=30   -------------------请求体,以表单形式提交参数
  • application/json

以json形式提交或返回数据,用在响应比较多。例如App访问服务器接口,服务器以json形式返回数据。 当然我们也可以以json形式提交参数,对应注解Body。

//定义
@FormUrlEncoded
@POST("http://www.hao123.com/")
Observable<ResponseBean> getInfo(@Body Request request);

//定义Request
class Request {
    public String name;
    public int age;
}


Request request = new Requst();
request.name = "test";
request.age = 30;
getInfo(request)

//提交数据
POST http://www.hao123.com/ HTTP/1.1 -------------------请求行
Content-Type: application/json;charset=utf-8 -------------------请求头
{"name":"test","age":30} -------------------请求体,以json形式提交参数

//以json形式返回数据
{
data: {},
msg: "操作成功",
code: 200
}

注意:传递RequestBody跟自定义实体,作用是一样的

RequestBody requestBody = RequestBody.create(MediaType.parse(MEDIA_TYPE),jsonObject.toString());
  • text/xml

以xml形式提交或返回数据,相对json,目前app用得比较少。

  • multipart/form-data

表示提交或返回二进制数据,例如图片、视频文件。