⚓ 回调函数
ForestRequest 提供了众多回调函数设置的方法
请求成功/失败回调函数
onSuccess(OnSuccess onSuccess)设置成功回调函数: 请求成功时被调用
- 参数
 onSuccess: OnSuccess 接口实例
onError(OnError onError)设置失败回调函数: 请求失败时被调用
- 参数
 onError: OnError 接口实例
Forest.post("/")
     // onSuccess回调函数: 请求成功时被调用
    .onSuccess(((data, req, res) -> {
        // data 为响应成功后返回的反序列化过的数据
        // req 为Forest请求对象,即 ForestRequest 类实例
        // res 为Forest响应对象,即 ForestResponse 类实例
    }))
     // onError回调函数: 请求失败时被调用
    .onError(((ex, req, res) -> {
        // ex 为请求过程可能抛出的异常对象
        // req 为Forest请求对象,即 ForestRequest 类实例
        // res 为Forest响应对象,即 ForestResponse 类实例
    }))
    // 执行请求,请求成功则执行onSuccess, 失败则执行onError
    .execute();
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
请求重试回调函数
onRetry(OnRetry onRetry)设置重试回调函数: 请求重试时被调用
- 参数
 onRetry: OnRetry 接口实例
Forest.post("/")
     // 设置最大请求重试次数为 3
    .maxRetryCount(3)
     // onRetry回调函数: 每次请求重试前被调用
    .onRetry(((req, res) -> {
        // req 为Forest请求对象,即 ForestRequest 类实例
        // res 为Forest响应对象,即 ForestResponse 类实例
    }))
    // 执行请求,请求失败会触发 onRetry, 然后重发请求
    .execute();
 1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
帮助我们改善此文档  (opens new window)
  上次更新: 2024/12/26, 12:59:11