2025-08-26 19:34:44 +08:00
|
|
|
package com.xscm.moduleutil.http;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
|
|
import com.blankj.utilcode.util.ThreadUtils;
|
|
|
|
|
import com.google.gson.JsonSyntaxException;
|
|
|
|
|
import com.hjq.toast.ToastUtils;
|
|
|
|
|
import com.xscm.moduleutil.base.CommonAppContext;
|
|
|
|
|
import com.xscm.moduleutil.event.LogOutEvent;
|
|
|
|
|
import com.xscm.moduleutil.utils.DialogUtils;
|
|
|
|
|
import com.xscm.moduleutil.utils.logger.Logger;
|
|
|
|
|
|
|
|
|
|
import org.apache.http.conn.ConnectTimeoutException;
|
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
|
|
|
|
|
|
import java.net.ConnectException;
|
|
|
|
|
import java.net.SocketTimeoutException;
|
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
import io.reactivex.Observer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by Administrator on 2017/11/22.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public abstract class BaseObserver<T> implements Observer<T> {
|
|
|
|
|
|
|
|
|
|
private boolean showErrMsg;
|
|
|
|
|
|
|
|
|
|
public BaseObserver() {
|
|
|
|
|
this(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BaseObserver(boolean showErrMsg) {
|
|
|
|
|
this.showErrMsg = showErrMsg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(Throwable e) {
|
|
|
|
|
if (e instanceof SocketTimeoutException) {
|
|
|
|
|
ToastUtils.show("网络中断,请检查您的网络状态");
|
|
|
|
|
} else if (e instanceof ConnectException) {
|
|
|
|
|
ToastUtils.show("网络中断,请检查您的网络状态");
|
|
|
|
|
} else if (e instanceof ConnectTimeoutException) {
|
|
|
|
|
ToastUtils.show("网络中断,请检查您的网络状态");
|
|
|
|
|
} else if (e instanceof UnknownHostException) {
|
|
|
|
|
ToastUtils.show("网络中断,请检查您的网络状态");
|
|
|
|
|
} else if (e instanceof IllegalStateException) {
|
|
|
|
|
ToastUtils.show("解析失败");
|
|
|
|
|
} else if (e instanceof APIException) {
|
|
|
|
|
APIException apiException = (APIException) e;
|
|
|
|
|
if (apiException.getCode() == 301) {
|
2025-09-24 00:30:40 +08:00
|
|
|
|
2025-08-26 19:34:44 +08:00
|
|
|
try {
|
2025-09-24 00:30:40 +08:00
|
|
|
ToastUtils.show(extractChinese(apiException.getMessage()));
|
|
|
|
|
// com.blankj.utilcode.util.ToastUtils.showShort("提示当前账号已在别的地方登陆,如果不是本人操作请修改密码");
|
|
|
|
|
EventBus.getDefault().post(new LogOutEvent());
|
2025-08-26 19:34:44 +08:00
|
|
|
CommonAppContext.getInstance().clearLoginInfo();
|
|
|
|
|
} catch (ClassNotFoundException ex) {
|
|
|
|
|
throw new RuntimeException(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (showErrMsg && !TextUtils.isEmpty(apiException.getMessage())) {
|
|
|
|
|
// ToastUtils.show(extractChinese(apiException.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
} else if (e instanceof APIException) {
|
|
|
|
|
APIException apiException = (APIException) e;
|
|
|
|
|
if (showErrMsg && !TextUtils.isEmpty(apiException.getMessage())) {
|
|
|
|
|
// ToastUtils.show(apiException.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} else if (e instanceof JsonSyntaxException) {
|
|
|
|
|
ToastUtils.show("网络请求错误……");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// ToastUtils.show(e.getMessage());
|
|
|
|
|
// CrashReport.postCatchedException(e); //bugly收集错误信息
|
|
|
|
|
}
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
onComplete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String extractChinese(String message) {
|
|
|
|
|
// 使用正则表达式提取中文字符
|
|
|
|
|
StringBuilder chinesePart = new StringBuilder();
|
|
|
|
|
Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+");
|
|
|
|
|
Matcher matcher = pattern.matcher(message);
|
|
|
|
|
while (matcher.find()) {
|
|
|
|
|
chinesePart.append(matcher.group());
|
|
|
|
|
}
|
|
|
|
|
return chinesePart.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onErrorCode(int code) {
|
|
|
|
|
Logger.e("onErrorCode", code);
|
|
|
|
|
if (code == 2) {
|
|
|
|
|
ThreadUtils.runOnUiThread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
DialogUtils.showNoBalance();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onComplete() {
|
|
|
|
|
Logger.i("onComplete");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
|
// public void onNext(BaseModel<T> response) {
|
|
|
|
|
// if (response.isSuccess()) {
|
|
|
|
|
// onSuccess(response.getData());
|
|
|
|
|
// } else if (response.isTokenExpired()) {
|
|
|
|
|
// // 触发退出登录
|
|
|
|
|
// handleTokenExpired();
|
|
|
|
|
// } else {
|
|
|
|
|
// onError(response.getMsg());
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// protected abstract void onSuccess(T data);
|
|
|
|
|
// protected abstract void onError(String msg);
|
|
|
|
|
// protected abstract void onFail(String error);
|
|
|
|
|
//
|
|
|
|
|
// @SneakyThrows
|
|
|
|
|
// private void handleTokenExpired() {
|
|
|
|
|
// // 退出登录逻辑
|
|
|
|
|
// ToastUtils.show("登录已过期,请重新登录");
|
|
|
|
|
// // 示例:跳转到登录页
|
|
|
|
|
// CommonAppContext.getInstance().clearLoginInfo();
|
|
|
|
|
// }
|
|
|
|
|
}
|