lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
package cn.sinata.xldutils.net.utils;
 
import com.google.gson.JsonSyntaxException;
 
import java.net.ConnectException;
import java.net.SocketTimeoutException;
 
import cn.sinata.xldutils.activitys.BaseActivity;
import cn.sinata.xldutils.bean.ResultData;
import cn.sinata.xldutils.fragment.BaseFragment;
import cn.sinata.xldutils.utils.Toast;
import cn.sinata.xldutils.utils.Utils;
import retrofit2.HttpException;
 
/**
 *
 */
public abstract class ResultDataSubscriber<T> extends ResultSubscriber<ResultData<T>> {
 
 
    public ResultDataSubscriber(BaseActivity activity) {
        super(activity);
 
    }
 
    public ResultDataSubscriber(BaseFragment fragment) {
        super(fragment);
 
    }
 
    @Override
    public void onNext(ResultData<T> tResultData) {
        super.onNext(tResultData);
        Utils.systemErr(tResultData);
        if (tResultData.getResult_code() == 1) {
            onSuccess(tResultData.getMessage(), tResultData.getData());
        } else {
            onError(new ResultException(tResultData.getResult_code(), tResultData.getMessage()));
        }
    }
 
    @Override
    public void onError(Throwable e) {
        super.onError(e);
        int code = -1;
        String msg = Error.REQUEST_ERROR;
        if (e instanceof JsonSyntaxException ||
                e instanceof NumberFormatException ||
                e instanceof IllegalStateException) {
            msg = Error.PARSER_ERROR;
        } else if (e instanceof HttpException) {
            msg = Error.SERVER_ERROR;
        } else if (e instanceof ConnectException) {
            msg = Error.NET_ERROR;
        } else if (e instanceof SocketTimeoutException) {
            msg = Error.NET_TIMEOUT;
        } else if (e instanceof ResultException) {
            code = ((ResultException) e).getCode();
            msg = e.getMessage();
        }
        onError(code, msg);
 
    }
 
    protected void onError(int code, String msg) {
        if (shouldShowErrorToast()) {
            showToast(msg);
        }
    }
 
    public abstract void onSuccess(String msg, T t);
 
    private void showToast(String msg) {
        if (activities != null) {
            BaseActivity activity = activities.get();
            if (activity != null) {
                Toast.create(activity).show(msg);
            }
        }
        if (fragments != null) {
            BaseFragment fragment = fragments.get();
            if (fragment != null) {
                Toast.create(fragment).show(msg);
            }
        }
 
    }
 
    protected boolean shouldShowErrorToast() {
        return true;
    }
}