BaseObserver.java
2.88 KB
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
package com.example.verificationcodejavademo.network;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;
import com.example.verificationcodejavademo.R;
import com.google.gson.Gson;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
/**
* Date:2020/5/18
* author:wuyan
* 对基础数据进行统一出路
*/
public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {
private static final String TAG = "BaseObserver";
private boolean mShowDialog;
private ProgressDialog dialog;
private Context mContext;
private Disposable d;
public abstract void onSuccess(T data);
public abstract void onFailure(Throwable e, String errorMsg);
public BaseObserver(Context mContext) {
this.mContext = mContext;
}
public BaseObserver(Context mContext, boolean mShowDialog) {
this.mContext = mContext;
this.mShowDialog = mShowDialog;
}
@Override
public void onSubscribe(Disposable d) {
this.d = d;
if (!isConnected(mContext)) {
Toast.makeText(mContext, "网络未连接", Toast.LENGTH_SHORT).show();
if (d.isDisposed()) {
d.dispose();
}
} else {
if (dialog == null && mShowDialog == true) {
dialog = new ProgressDialog(mContext, R.style.dialog);
dialog.show();
}
}
}
//后给给返回结果
@Override
public void onNext(BaseResponse<T> response) {
Log.i(TAG, "网络请求数据结果: " + new Gson().toJson(response));
if (response.getRepCode().equals("0000")) {
onSuccess(response.getRepData());
} else {
//返回其他错误码
onFailure(null, "网络请求失败");
}
}
//网络请求失败
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
if (d.isDisposed()) {
d.dispose();
}
hidDialog();
onFailure(e, RxExceptionUtil.exceptionHandler(e));
}
@Override
public void onComplete() {
if (d.isDisposed()) {
d.dispose();
}
hidDialog();
}
private void hidDialog() {
if (dialog != null && mShowDialog == true) {
dialog.dismiss();
dialog = null;
}
}
/**
* 是否又网络连接
*/
private static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null) {
return false;
}
boolean available = info.isAvailable();
return available;
}
}