反序列化#
Forest请求会自动将响应的返回数据反序列化成您要的数据类型。想要接受指定类型的数据需要完成两步操作:
第一步:定义dataType
属性
dataType
属性指定了该请求响应返回的数据类型,目前可选的数据类型有三种: text
, json
, xml
Forest会根据您指定的dataType
属性选择不同的反序列化方式。其中dataType
的默认值为text
,如果您不指定其他数据类型,那么Forest就不会做任何形式的序列化,并以文本字符串的形式返回给你数据。
@Request(
url = "http://localhost:8080/text/data",
dataType = "text"
)
String getData();
若您指定为json
或xml
,那就告诉了Forest该请求的响应数据类型为JSON或XML形式的数据,就会以相应的形式进行反序列化。
@Request(
url = "http://localhost:8080/text/data",
dataType = "json"
)
Map getData();
第二步:指定反序列化的目标类型
反序列化需要一个目标类型,而该类型其实就是方法的返回值类型,如返回值为String
就会反序列成String
字符串,返回值为Map
就会反序列化成一个HashMap对象,您也可以指定为自定义的Class类型。
public class User {
private String username;
private String score;
}
如有上面这样的User类,并把它指定为方法的返回类型,而且相应返回的数据这样一段JSON:
{"username": "Foo", "score": "82"}
那请求接口就应该定义成这样:
@Get(
url = "http://localhost:8080/user?id=${0}",
dataType = "json"
)
User getUser(Integer id)
从1.4.0
版本开始,dataType
属性默认为 auto
(自动判断数据类型), 也就是说 dataType
属性可以完全省略不填,Forest会自行判断返回的数据类型是哪种格式。
@Get("http://localhost:8080/user?id=${0}")
User getUser(Integer id)
返回响应对象#
直接用普通的对象类型作为请求方法的返回类型,可以将响应数据方便的反序列化,以满足大部分的需求。但还有很多时候不光需要获取响应内容,也需要得到响应头等信息,这时候就需要 ForestResponse
出场了。
将ForestResponse
作为请求方法的返回值类型
@Post("http://localhost:8080/user")
ForestResponse<String> postUser(@JSONBody User user);
用ForestResponse
对象接到请求响应数据后便可以获取响应内容
ForestResponse<String> response = client.postUser(user);
if (response.isError()) {
... ...
}
if (response.isSuccess()) {
... ...
}
String text = response.readAsString();
String content = response.getContent();
String result = response.getResult();
byte[] byteArray = response.getByteArray();
InputStream in = response.getInputStream();
因为ForestResponse
为带泛型的类型,其泛型参数可以是任何其他类型,所以可以根据它的泛型参数中的类型不同,而将响应内容反序列化成不同的对象。
@Get("http://localhost:8080/user")
ForestResponse<User> getUser(@Query("id") String userId);
同样是用ForestResponse
类型变量去接受响应数据
ForestResponse<User> response = client.postUser(user);
if (response.isSuccess()) {
User user = response.getResult();
}
拦截器中获取响应对象#
public class SimpleInterceptor1 implements Interceptor<String> {
... ...
@Override
public void afterExecute(ForestRequest request, ForestResponse response) {
int status = response.getStatusCode();
String content = response.getContent();
String result = response.getResult();
}
}
回调函数中获取响应对象#
@Post("http://localhost:8080/user")
void postUser(@JSONBody User user, OnSuccess<String> onSuccess);
... ...
client.postUser(user, (String resText, ForestRequest request, ForestResponse response) -> {
int status = response.getStatusCode();
String content = response.getContent();
String result = response.getResult();
});
获取响应头#
要获取响应头首先要获取响应对象,也就是ForestResponse
对象,这一步可以参见《获取响应对象》。
获取ForestResponse
对象后便可以获取响应头了
ForestResponse<String> response = client.textXXX();
ForestHeader header = response.getHeader("Content-Type");
String headerName = header.getName();
String headerValue = header.getValue();
List<ForestHeader> heaers = response.getHeaders("Content-Type");
String val = response.getHeaderValue("Content-Type");
List<String> vals = response.getHeaderValues("Content-Type");