接口 Interceptor<T>
- 所有超级接口:
OnCanceled,OnError,OnLoadCookie,OnProgress,OnRedirection,OnRetry,OnSaveCookie,OnSuccess<T>
- 所有已知子接口:
BaseAnnotationLifeCycle<A,,I> MethodAnnotationLifeCycle<A,,I> ParameterAnnotationLifeCycle<A,I>
- 所有已知实现类:
AbstractBodyLifeCycle,AddressLifeCycle,BackendClientLifeCycle,BackendLifeCycle,BaseLogEnabledLifeCycle,BaseLogHandlerLifeCycle,BaseRequestLifeCycle,BasicAuthLifeCycle,BinaryBodyLifeCycle,BodyLifeCycle,BodyTypeLifeCycle,DataFileLifeCycle,DataObjectLifeCycle,DataParamLifeCycle,DecompressGzipLifeCycle,DeleteRequestLifeCycle,DownloadLifeCycle,FormBodyLifeCycle,GetRequestLifeCycle,HeaderLifeCycle,HeadersLifeCycle,HeadRequestLifeCycle,HttpClientLifeCycle,HTTPProxyLifeCycle,InterceptorChain,JSONBodyLifeCycle,JSONQueryLifeCycle,LogEnabledLifeCycle,LogHandlerLifeCycle,OAuth2LifeCycle,OkHttp3LifeCycle,OptionsRequestLifeCycle,PatchRequestLifeCycle,PostRequestLifeCycle,ProtobufBodyLifeCycle,PutRequestLifeCycle,QueryLifeCycle,RedirectionLifeCycle,RequestLifeCycle,RetryerLifeCycle,RetryLifeCycle,ReturnLifeCycle,SSLHostnameVerifierLifeCycle,SSLSocketFactoryBuilderLifeCycle,SuccessLifeCycle,TraceRequestLifeCycle,URLEncodeLifeCycle,VariableLifeCycle,XMLBodyLifeCycle
public interface Interceptor<T>
extends OnSuccess<T>, OnError, OnCanceled, OnProgress, OnLoadCookie, OnSaveCookie, OnRetry, OnRedirection
Forest拦截器接口
拦截器在请求的初始化、发送请求前、发送成功、发送失败等生命周期中都会被调用
总的生命周期回调函数调用顺序如下:
Forest接口方法调用 ->
¦ onInvokeMethod ->
¦ beforeExecute ->
¦ 如果返回 false -> 中断请求,直接返回
¦ 如果返回 true ->
¦ 发送请求 ->
¦ 发送请求失败 ->
¦ retryWhen ->
¦ 返回 true 则触发请求重试
¦ 返回 false 则跳转到 [onError]
¦ onError -> 跳转到 [afterExecute]
¦ 发送请求成功 ->
¦ 等待响应 ->
¦ 接受到响应 ->
¦ retryWhen ->
¦ 返回 true 则触发请求重试
¦ 返回 false 判断响应状态 ->
¦ 响应失败 -> onError -> 跳转到 [afterExecute]
¦ 响应成功 -> onSuccess -> 跳转到 [afterExecute]
¦ afterExecute -> 退出 Forest 接口方法,并返回数据
- 从以下版本开始:
- 2016-06-26
- 作者:
- gongjun[dt_flys@hotmail.com]
-
方法概要
修饰符和类型方法说明default voidaddAttribute(ForestRequest request, String name, Object value) 添加请求在本拦截器中的 Attribute 属性default voidafterExecute(ForestRequest request, ForestResponse response) 默认回调函数: 请求完成后(成功/失败后) 调用该方法default booleanbeforeExecute(ForestRequest request) 默认回调函数: 请求执行前调用该方法default ObjectgetAttribute(ForestRequest request, String name) 获取请求在本拦截器中的 Attribute 属性default <T> TgetAttribute(ForestRequest request, String name, Class<T> clazz) 按自义定类型获取请求在本拦截器中的 Attribute 属性default DoublegetAttributeAsDouble(ForestRequest request, String name) 按双精度浮点数类型获取请求在本拦截器中的 Attribute 属性default FloatgetAttributeAsFloat(ForestRequest request, String name) 按单精度浮点数类型获取请求在本拦截器中的 Attribute 属性default IntegergetAttributeAsInteger(ForestRequest request, String name) 按整数类型获取请求在本拦截器中的 Attribute 属性default StringgetAttributeAsString(ForestRequest request, String name) 按字符串类型获取请求在本拦截器中的 Attribute 属性default InterceptorAttributesgetAttributes(ForestRequest request) 获取请求在本拦截器中的 Attribute 属性default byte[]onBodyEncode(ForestRequest request, ForestEncoder encoder, byte[] encodedData) 在请求体数据序列化后,发送请求数据前调用该方法default voidonCanceled(ForestRequest req, ForestResponse res) 默认回调函数: 请求取消后调用该方法default voidonError(ForestRuntimeException ex, ForestRequest request, ForestResponse response) 默认回调函数: 请求失败后调用该方法default voidonInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) 默认回调函数: 接口方法执行时调用该方法default voidonLoadCookie(ForestRequest request, ForestCookies cookies) 默认回调函数: 在发送请求加载Cookie时调用该方法default voidonProgress(ForestProgress progress) 默认文件上传或下载监听传输进度时调用该方法default voidonRedirection(ForestRequest<?> redirectReq, ForestRequest<?> prevReq, ForestResponse<?> prevRes) 默认回调函数:在请求重定向时触发default voidonRetry(ForestRequest request, ForestResponse response) 默认回调函数: 在触发请求重试时执行default voidonSaveCookie(ForestRequest request, ForestCookies cookies) 默认回调函数: 在请求响应成功后,需要保存Cookie时调用该方法default voidonSuccess(T data, ForestRequest request, ForestResponse response) 默认回调函数: 请求成功后调用该方法
-
方法详细资料
-
onInvokeMethod
默认回调函数: 接口方法执行时调用该方法默认为什么都不做
- 参数:
request- Forest请求对象method- Forest方法对象args- 方法调用入参数组
-
beforeExecute
默认回调函数: 请求执行前调用该方法其返回值为布尔类型,可以控制请求是否继续执行
默认为什么都不做
- 参数:
request- Forest请求对象- 返回:
true: 继续执行该请求, 否则中断请求
-
afterExecute
默认回调函数: 请求完成后(成功/失败后) 调用该方法默认为什么都不做
- 参数:
request- Forest请求对象response- Forest响应对象
-
onBodyEncode
在请求体数据序列化后,发送请求数据前调用该方法默认为什么都不做
注: mutlipart/data类型的文件上传格式的 Body 数据不会调用该回调函数
- 参数:
request- Forest请求对象encoder- Forest转换器encodedData- 序列化后的请求体数据
-
onSuccess
默认回调函数: 请求成功后调用该方法默认为什么都不做
-
onError
默认回调函数: 请求失败后调用该方法默认为什么都不做
-
onCanceled
默认回调函数: 请求取消后调用该方法默认为什么都不做
- 指定者:
onCanceled在接口中OnCanceled- 参数:
req- Forest请求对象res- Forest响应对象
-
onRetry
默认回调函数: 在触发请求重试时执行默认为什么都不做
-
onProgress
默认文件上传或下载监听传输进度时调用该方法默认为什么都不做
- 指定者:
onProgress在接口中OnProgress- 参数:
progress- Forest进度对象
-
onRedirection
default void onRedirection(ForestRequest<?> redirectReq, ForestRequest<?> prevReq, ForestResponse<?> prevRes) 默认回调函数:在请求重定向时触发默认为什么都不做
- 指定者:
onRedirection在接口中OnRedirection- 参数:
redirectReq- 进行重定向的新请求prevReq- 上一个请求prevRes- 上一个请求的响应
-
onLoadCookie
默认回调函数: 在发送请求加载Cookie时调用该方法默认为什么都不做
- 指定者:
onLoadCookie在接口中OnLoadCookie- 参数:
request- Forest请求对象cookies- Cookie集合, 需要通过请求发送的Cookie都添加到该集合
-
onSaveCookie
默认回调函数: 在请求响应成功后,需要保存Cookie时调用该方法默认为什么都不做
- 指定者:
onSaveCookie在接口中OnSaveCookie- 参数:
request- Forest请求对象cookies- Cookie集合,通过响应返回的Cookie都从该集合获取
-
getAttributes
获取请求在本拦截器中的 Attribute 属性- 参数:
request- Forest请求对象- 返回:
InterceptorAttributes对象实例
-
addAttribute
添加请求在本拦截器中的 Attribute 属性- 参数:
request- Forest请求对象name- 属性名称value- 属性值
-
getAttribute
获取请求在本拦截器中的 Attribute 属性- 参数:
request- Forest请求对象name- 属性名称- 返回:
- 属性值
-
getAttribute
按自义定类型获取请求在本拦截器中的 Attribute 属性- 类型参数:
T- 属性值类型的泛型- 参数:
request- Forest请求对象name- 属性名称clazz- 属性值的类型对象- 返回:
- Attribute 属性值
-
getAttributeAsString
按字符串类型获取请求在本拦截器中的 Attribute 属性- 参数:
request- Forest请求对象name- 属性名称- 返回:
- 字符串类型属性值
-
getAttributeAsInteger
按整数类型获取请求在本拦截器中的 Attribute 属性- 参数:
request- Forest请求对象name- 属性名称- 返回:
- 整数类型属性值
-
getAttributeAsFloat
按单精度浮点数类型获取请求在本拦截器中的 Attribute 属性- 参数:
request- Forest请求对象name- 属性名称- 返回:
- 单精度浮点数类型属性值
-
getAttributeAsDouble
按双精度浮点数类型获取请求在本拦截器中的 Attribute 属性- 参数:
request- Forest请求对象name- 属性名称- 返回:
- 双精度浮点数类型属性值
-