riku
2025-07-02 3013b813e5df6977c0be921928f73b1a3adde290
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package cn.flightfeather.thirdappmodule.common.net
 
import android.accounts.NetworkErrorException
import cn.flightfeather.thirdappmodule.model.bean.BaseResponse
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import retrofit2.Response
import java.net.ConnectException
import java.net.UnknownHostException
import java.util.concurrent.TimeoutException
 
/**
 * @author riku
 * Date: 2019/4/24
 * RxJava2 网络请求返回处理
 */
abstract class ResultObserver<T>:Observer<Response<T>> {
 
    override fun onSubscribe(d: Disposable) {
        if (!d.isDisposed) {
            onRequestStart()
        }
    }
 
    override fun onNext(response: Response<T>) {
        getPageInfo(response)
        onRequestEnd()
        if (response.isSuccessful) {
            try {
                onSuccess(response.body())
            } catch (e: Exception) {
                e.printStackTrace()
            }
 
        } else {
            try {
                onInnerCodeError(response.code(), response.message())
                onCodeError(response.code(), response.message())
                onFailure(Throwable(), false)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
 
    override fun onError(e: Throwable) {
        onRequestEnd()
        try {
            if (e is ConnectException
                || e is TimeoutException
                || e is NetworkErrorException
                || e is UnknownHostException
            ) {
                onFailure(e, true)
            } else {
                onFailure(e, false)
            }
        } catch (e1: Exception) {
            e1.printStackTrace()
        }
    }
 
    override fun onComplete() = Unit
 
 
    private fun getPageInfo(response: Response<T>) {
        var currentPage = -1
        var totalPage = -1
        try {
            val body = response.body()
            if (body is BaseResponse<*>) {
                currentPage = body.head?.page ?: -1
                totalPage = body.head?.totalPage ?: -1
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        onPage(currentPage, totalPage)
    }
 
 
    /***********自定义回调方法***********/
    /**
     * 请求开始
     */
    open fun onRequestStart() = Unit
 
    /**
     * 获取到分页信息
     */
    open fun onPage(current: Int, total: Int) = Unit
 
    /**
     * 请求结束
     */
    open fun onRequestEnd() = Unit
 
    /**
     * 网络请求返回成功,但code错误
     */
    open fun onInnerCodeError(code: Int, message: String) = Unit
 
    /**
     * 提供网络请求返回错误代码时的自定义处理方法
     */
    open fun onCodeError(code: Int, message: String) = Unit
 
    /**
     * 请求成功
     */
    @Throws(Exception::class)
    abstract fun onSuccess(result: T?)
 
    /**
     * 请求失败
     */
    @Throws(java.lang.Exception::class)
    abstract fun onFailure(e: Throwable, isNetWorkError: Boolean)
 
}