Files
midi-android/moduleUtil/src/main/java/com/xscm/moduleutil/http/BaseObserver.java
梁小江 5f573e607d 1:修改交友房
2:修改拍卖房
3:修改飘屏(没有修改成功)
2025-09-24 00:30:40 +08:00

137 lines
4.6 KiB
Java

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) {
try {
ToastUtils.show(extractChinese(apiException.getMessage()));
// com.blankj.utilcode.util.ToastUtils.showShort("提示当前账号已在别的地方登陆,如果不是本人操作请修改密码");
EventBus.getDefault().post(new LogOutEvent());
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();
// }
}