lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.kuanzhai.driver.netUtls;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
 
import com.kuanzhai.driver.bean.FaceBean;
import com.kuanzhai.driver.utils.LogUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
 
import java.lang.ref.WeakReference;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
 
import cn.sinata.xldutils.activitys.BaseActivity;
import cn.sinata.xldutils.fragment.BaseFragment;
import cn.sinata.xldutils.net.utils.ResultException;
import cn.sinata.xldutils.utils.StringUtils;
import cn.sinata.xldutils.utils.Toast;
import cn.sinata.xldutils.utils.Utils;
import cn.sinata.xldutils.xldUtils;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
import retrofit2.HttpException;
 
/**
 * 自定义Observer类处理网络数据
 * Created by Administrator on 2018/1/25.
 */
 
public abstract class FaceObserver implements Observer<ResponseBody>, Disposable {
 
    WeakReference<BaseActivity> activities;
    WeakReference<BaseFragment> fragments;
    Context context;
 
    public static final String REQUEST_ERROR = "请求出错!";
    public static final String PARSER_ERROR = "数据解析出错!";
    public static final String SERVER_ERROR = "服务器异常,请稍后重试!";
    public static final String NET_ERROR = "未连接到服务器!";
    public static final String NET_TIMEOUT = "链接超时,请稍后再试!";
 
 
    public FaceObserver(BaseActivity activity) {
        activities = new WeakReference<>(activity);
        if (activity != null) {
            this.context = activity;
            activity.compositeSubscription.add(this);
        }
    }
 
    public FaceObserver(BaseFragment fragment) {
        fragments = new WeakReference<BaseFragment>(fragment);
        if (fragment != null && !fragment.isDetached()) {
            this.context = fragment.getContext();
            fragment.addDisposable(this);
        }
    }
 
    public FaceObserver(Context context) {
        this.context = context;
    }
 
    @Override
    public void onSubscribe(@NonNull Disposable d) {
 
    }
 
    @Override
    public void onNext(@NonNull ResponseBody responseBody) {
        dismissDialog();
        try {
            String responseString = responseBody.string();
            LogUtils.e("responseString", "**" + responseString);
            if (StringUtils.isEmpty(responseString)) { //判空
                int code = -1;
                String msg = PARSER_ERROR;
                onError(code, msg,responseString);
                return;
            }
//            success(responseString);
            FaceBean baseBean = new Gson().fromJson(responseString, FaceBean.class);
            if (baseBean.getStatus() == Api.SUCCESS) {
                success(responseString);
            } else {
//                onError(baseBean.getStatus(), baseBean.getMsg(),responseString);
//                success(responseString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public void onError(@NonNull Throwable e) {
        dismissDialog();
        if (xldUtils.DEBUG) {
            e.printStackTrace();
        }
        Utils.systemErr(e);
        Log.e("responseString", "responseString get  " + e.toString());
        int code = -1;
        String msg = REQUEST_ERROR;
        if (e instanceof JsonSyntaxException ||
                e instanceof NumberFormatException ||
                e instanceof IllegalStateException) {
            msg = PARSER_ERROR;
        } else if (e instanceof HttpException) {
            msg = SERVER_ERROR;
        } else if (e instanceof ConnectException) {
            msg = NET_ERROR;
        } else if (e instanceof SocketTimeoutException) {
            msg = NET_TIMEOUT;
        } else if (e instanceof ResultException) {
            code = ((ResultException) e).getCode();
            msg = e.getMessage();
        }
        try {
            onError(code, msg,"");
        } catch (Exception es) {
            es.printStackTrace();
        }
 
    }
 
    protected void onError(int code, String msg,String resposeString) {
        if (shouldShowErrorToast()) {
            showToast(msg);
        }
    }
 
    protected boolean shouldShowErrorToast() {
        return true;
    }
 
    @Override
    public void onComplete() {
 
    }
 
    /**
     * 显示toast
     *
     * @param s 提示文字
     */
    protected void showToast(String s) {
        Toast.create(context).show(s);
    }
 
    /**
     * 访问成功回调抽象方法
     *
     * @param responseString 返回的数据
     */
    public abstract void success(String responseString);
 
    /**
     * 访问成功回调方法
     *
     * @param bitmap 获取的Bitmap
     */
    public void success(Bitmap bitmap) {
    }
 
    private void dismissDialog() {
        if (activities != null) {
            BaseActivity activity = activities.get();
            if (activity != null) {
                activity.dismissDialog();
 
            }
        }
        if (fragments != null) {
            BaseFragment fragment = fragments.get();
            if (fragment != null) {
                fragment.dismissDialog();
 
            }
        }
    }
 
    @Override
    public boolean isDisposed() {
        return false;
    }
 
    @Override
    public void dispose() {
 
    }
}