接口 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]
  • 方法详细资料

    • onInvokeMethod

      default void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args)
      默认回调函数: 接口方法执行时调用该方法

      默认为什么都不做

      参数:
      request - Forest请求对象
      method - Forest方法对象
      args - 方法调用入参数组
    • beforeExecute

      default boolean beforeExecute(ForestRequest request)
      默认回调函数: 请求执行前调用该方法

      其返回值为布尔类型,可以控制请求是否继续执行

      默认为什么都不做

      参数:
      request - Forest请求对象
      返回:
      true: 继续执行该请求, 否则中断请求
    • afterExecute

      default void afterExecute(ForestRequest request, ForestResponse response)
      默认回调函数: 请求完成后(成功/失败后) 调用该方法

      默认为什么都不做

      参数:
      request - Forest请求对象
      response - Forest响应对象
    • onBodyEncode

      default byte[] onBodyEncode(ForestRequest request, ForestEncoder encoder, byte[] encodedData)
      在请求体数据序列化后,发送请求数据前调用该方法

      默认为什么都不做

      注: mutlipart/data类型的文件上传格式的 Body 数据不会调用该回调函数

      参数:
      request - Forest请求对象
      encoder - Forest转换器
      encodedData - 序列化后的请求体数据
    • onSuccess

      default void onSuccess(T data, ForestRequest request, ForestResponse response)
      默认回调函数: 请求成功后调用该方法

      默认为什么都不做

      指定者:
      onSuccess 在接口中 OnSuccess<T>
      参数:
      data - 请求响应返回后经过序列化后的数据
      request - Forest请求对象
      response - Forest响应对象
    • onError

      default void onError(ForestRuntimeException ex, ForestRequest request, ForestResponse response)
      默认回调函数: 请求失败后调用该方法

      默认为什么都不做

      指定者:
      onError 在接口中 OnError
      参数:
      ex - 请求失败的异常对象
      request - Forest请求对象
      response - Forest响应对象
    • onCanceled

      default void onCanceled(ForestRequest req, ForestResponse res)
      默认回调函数: 请求取消后调用该方法

      默认为什么都不做

      指定者:
      onCanceled 在接口中 OnCanceled
      参数:
      req - Forest请求对象
      res - Forest响应对象
    • onRetry

      default void onRetry(ForestRequest request, ForestResponse response)
      默认回调函数: 在触发请求重试时执行

      默认为什么都不做

      指定者:
      onRetry 在接口中 OnRetry
      参数:
      request - Forest请求对象
      response - Forest响应对象
    • onProgress

      default void onProgress(ForestProgress progress)
      默认文件上传或下载监听传输进度时调用该方法

      默认为什么都不做

      指定者:
      onProgress 在接口中 OnProgress
      参数:
      progress - Forest进度对象
    • onRedirection

      default void onRedirection(ForestRequest<?> redirectReq, ForestRequest<?> prevReq, ForestResponse<?> prevRes)
      默认回调函数:在请求重定向时触发

      默认为什么都不做

      指定者:
      onRedirection 在接口中 OnRedirection
      参数:
      redirectReq - 进行重定向的新请求
      prevReq - 上一个请求
      prevRes - 上一个请求的响应
    • onLoadCookie

      default void onLoadCookie(ForestRequest request, ForestCookies cookies)
      默认回调函数: 在发送请求加载Cookie时调用该方法

      默认为什么都不做

      指定者:
      onLoadCookie 在接口中 OnLoadCookie
      参数:
      request - Forest请求对象
      cookies - Cookie集合, 需要通过请求发送的Cookie都添加到该集合
    • onSaveCookie

      default void onSaveCookie(ForestRequest request, ForestCookies cookies)
      默认回调函数: 在请求响应成功后,需要保存Cookie时调用该方法

      默认为什么都不做

      指定者:
      onSaveCookie 在接口中 OnSaveCookie
      参数:
      request - Forest请求对象
      cookies - Cookie集合,通过响应返回的Cookie都从该集合获取
    • getAttributes

      default InterceptorAttributes getAttributes(ForestRequest request)
      获取请求在本拦截器中的 Attribute 属性
      参数:
      request - Forest请求对象
      返回:
      InterceptorAttributes 对象实例
    • addAttribute

      default void addAttribute(ForestRequest request, String name, Object value)
      添加请求在本拦截器中的 Attribute 属性
      参数:
      request - Forest请求对象
      name - 属性名称
      value - 属性值
    • getAttribute

      default Object getAttribute(ForestRequest request, String name)
      获取请求在本拦截器中的 Attribute 属性
      参数:
      request - Forest请求对象
      name - 属性名称
      返回:
      属性值
    • getAttribute

      default <T> T getAttribute(ForestRequest request, String name, Class<T> clazz)
      按自义定类型获取请求在本拦截器中的 Attribute 属性
      类型参数:
      T - 属性值类型的泛型
      参数:
      request - Forest请求对象
      name - 属性名称
      clazz - 属性值的类型对象
      返回:
      Attribute 属性值
    • getAttributeAsString

      default String getAttributeAsString(ForestRequest request, String name)
      按字符串类型获取请求在本拦截器中的 Attribute 属性
      参数:
      request - Forest请求对象
      name - 属性名称
      返回:
      字符串类型属性值
    • getAttributeAsInteger

      default Integer getAttributeAsInteger(ForestRequest request, String name)
      按整数类型获取请求在本拦截器中的 Attribute 属性
      参数:
      request - Forest请求对象
      name - 属性名称
      返回:
      整数类型属性值
    • getAttributeAsFloat

      default Float getAttributeAsFloat(ForestRequest request, String name)
      按单精度浮点数类型获取请求在本拦截器中的 Attribute 属性
      参数:
      request - Forest请求对象
      name - 属性名称
      返回:
      单精度浮点数类型属性值
    • getAttributeAsDouble

      default Double getAttributeAsDouble(ForestRequest request, String name)
      按双精度浮点数类型获取请求在本拦截器中的 Attribute 属性
      参数:
      request - Forest请求对象
      name - 属性名称
      返回:
      双精度浮点数类型属性值