lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
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
package cn.sinata.xldutils.rxutils
 
import cn.sinata.xldutils.data.ResultData
import cn.sinata.xldutils.sysErr
import com.google.gson.JsonSyntaxException
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException
 
/**
 * 服务器返回数据基本处理
 */
abstract class ResultDataSubscriber<T>(helper: RequestHelper) : ResultSubscriber<ResultData<T>>(helper) {
 
    override fun onNext(t: ResultData<T>) {
        sysErr(t.msg+"---------"+t.data+"-----"+t.code)
        SystemUtil.sysTime = t.xtTime!!
        if (t.code == 200) {
            if (t.msg == null) {
                t.msg=""
            }
            onSuccess(t.msg,t.data)
        }
        else {
            onError(ResultException(t.code, t.msg))
        }
    }
 
    /**
     * 默认错误方法。
     */
    override fun onError(t: Throwable) {
        super.onError(t)
        sysErr(t.message)
        t.printStackTrace()
        var code = -1
        var msg = Error.REQUEST_ERROR
        when (t) {
            is JsonSyntaxException -> msg = Error.PARSER_ERROR
            is ConnectException -> msg = Error.NET_ERROR
            is SocketTimeoutException -> msg = Error.NET_ERROR
            is HttpException -> msg = Error.SERVER_ERROR
            is ResultException -> {
                code = t.code
                msg = t.message!!
            }
 
        }
        onError(code, msg)
    }
 
    /**
     * 错误时处理。如有特殊code判断,复写此方法
     */
    open fun onError(code: Int, msg: String) {
        if (isShowToast()) {
            _showToast(msg)
        }
    }
 
    private fun _showToast(msg: String) {
        helper?.errorToast(msg)
    }
 
    /**
     * 默认展示错误Toast
     */
    open fun isShowToast(): Boolean = true
 
    /**
     * 服务器返回成功code时数据
     */
    abstract fun onSuccess(msg: String?, data: T?)
}