[Modify]修改网络错误提示
This commit is contained in:
@@ -4,8 +4,8 @@ import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.yizhuan.xchat_android_core.utils.Logger
|
||||
import com.yizhuan.xchat_android_core.utils.toast
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.exception.ExceptionHandle
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@@ -18,7 +18,8 @@ open class BaseViewModel : ViewModel() {
|
||||
needLoading: Boolean = false,
|
||||
onError: (e: Throwable) -> Unit = {
|
||||
if (it.message != "Job was cancelled") {
|
||||
it.message.toast()
|
||||
val message = ExceptionHandle.handleException(it)
|
||||
message.toast()
|
||||
}
|
||||
},
|
||||
onComplete: (() -> Unit)? = null,
|
||||
|
@@ -9,13 +9,13 @@ import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.yizhuan.erban.R;
|
||||
import com.yizhuan.erban.base.BaseActivity;
|
||||
import com.yizhuan.xchat_android_core.PreferencesUtils;
|
||||
import com.yizhuan.xchat_android_core.auth.AuthModel;
|
||||
import com.yizhuan.xchat_android_core.auth.exception.AccountCancelException;
|
||||
import com.yizhuan.xchat_android_core.auth.exception.BanAccountException;
|
||||
import com.yizhuan.xchat_android_core.auth.exception.IsSuperAdminException;
|
||||
import com.yizhuan.xchat_android_core.statistic.StatisticManager;
|
||||
import com.yizhuan.xchat_android_core.statistic.protocol.StatisticsProtocol;
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.exception.ExceptionHandle;
|
||||
import com.yizhuan.xchat_android_library.utils.ResUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -68,7 +68,8 @@ public class LogoutHelper {
|
||||
} else if (e instanceof IOException) {
|
||||
activity.toast(ResUtil.getString(R.string.login_helper_logouthelper_013));
|
||||
} else {
|
||||
activity.toast(e.getMessage());
|
||||
String message = ExceptionHandle.Companion.handleException(e);
|
||||
activity.toast(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -5,11 +5,11 @@ import android.content.Context;
|
||||
import com.yizhuan.xchat_android_core.R;
|
||||
import com.yizhuan.xchat_android_core.bean.response.ServiceResult;
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.callback.CallBack;
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.exception.ExceptionHandle;
|
||||
import com.yizhuan.xchat_android_library.utils.ResUtil;
|
||||
import com.yizhuan.xchat_android_library.utils.config.BasicConfig;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.ObservableSource;
|
||||
@@ -57,12 +57,7 @@ public abstract class BaseModel implements IModel {
|
||||
public void onError(Throwable e) {
|
||||
e.printStackTrace();
|
||||
if (callBack != null) {
|
||||
String error = e.getMessage();
|
||||
if (e instanceof UnknownHostException) {
|
||||
error = ResUtil.getString(R.string.xchat_android_core_base_basemodel_02);
|
||||
}else if (e instanceof TimeoutException){
|
||||
error = ResUtil.getString(R.string.xchat_android_core_base_basemodel_03);
|
||||
}
|
||||
String error = ExceptionHandle.Companion.handleException(e);
|
||||
callBack.onFail(-1, error);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,20 @@
|
||||
package com.yizhuan.xchat_android_library.net.rxnet.exception
|
||||
|
||||
/**
|
||||
* Created by qisan 2022/5/26
|
||||
* com.qisan.wanandroid.http.exception
|
||||
*/
|
||||
class ApiException : RuntimeException {
|
||||
|
||||
private var code: Int? = null
|
||||
|
||||
constructor(throwable: Throwable, code: Int) : super(throwable) {
|
||||
this.code = code
|
||||
}
|
||||
|
||||
constructor(message: String) : super(Throwable(message))
|
||||
|
||||
constructor(message: String,code: Int) : super(Throwable(message)){
|
||||
this.code = code
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package com.yizhuan.xchat_android_library.net.rxnet.exception
|
||||
|
||||
/**
|
||||
* Created by qisan 2022/5/26
|
||||
* com.qisan.wanandroid.http.exception
|
||||
*/
|
||||
object ErrorStatus {
|
||||
/**
|
||||
* 响应成功
|
||||
*/
|
||||
const val SUCCESS = 0
|
||||
|
||||
/**
|
||||
* Token 过期
|
||||
*/
|
||||
const val TOKEN_INVALID = 401
|
||||
|
||||
/**
|
||||
* 未知错误
|
||||
*/
|
||||
const val UNKNOWN_ERROR = 1002
|
||||
|
||||
/**
|
||||
* 服务器内部错误
|
||||
*/
|
||||
const val SERVER_ERROR = 1003
|
||||
|
||||
/**
|
||||
* 网络连接超时
|
||||
*/
|
||||
const val NETWORK_ERROR = 1004
|
||||
|
||||
/**
|
||||
* API解析异常(或者第三方数据结构更改)等其他异常
|
||||
*/
|
||||
const val API_ERROR = 1005
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
package com.yizhuan.xchat_android_library.net.rxnet.exception
|
||||
|
||||
import android.util.Log
|
||||
import com.google.gson.JsonParseException
|
||||
import org.json.JSONException
|
||||
import retrofit2.HttpException
|
||||
import java.net.ConnectException
|
||||
import java.net.SocketTimeoutException
|
||||
import java.net.UnknownHostException
|
||||
import java.text.ParseException
|
||||
|
||||
/**
|
||||
* Created by qisan 2022/5/26
|
||||
* com.qisan.wanandroid.http.exception
|
||||
*/
|
||||
class ExceptionHandle {
|
||||
companion object {
|
||||
private const val TAG = "ExceptionHandle"
|
||||
var errorCode = ErrorStatus.UNKNOWN_ERROR
|
||||
var errorMsg = "請求失敗,請稍後重試"
|
||||
|
||||
fun handleException(e: Throwable): String {
|
||||
e.printStackTrace()
|
||||
when (e) {
|
||||
is SocketTimeoutException, is ConnectException, is HttpException -> { //均视为网络错误
|
||||
Log.e(TAG, "網絡連接異常: " + e.message)
|
||||
errorMsg = "網絡異常,請檢查您的網絡再試~"
|
||||
errorCode = ErrorStatus.NETWORK_ERROR
|
||||
}
|
||||
is JsonParseException, is JSONException, is ParseException -> { //均视为解析错误
|
||||
Log.e(TAG, "數據解析異常: " + e.message)
|
||||
errorMsg = "數據解析異常"
|
||||
errorCode = ErrorStatus.SERVER_ERROR
|
||||
}
|
||||
is ApiException -> {//服务器返回的错误信息
|
||||
errorMsg = e.message.toString()
|
||||
errorCode = ErrorStatus.SERVER_ERROR
|
||||
}
|
||||
is UnknownHostException -> {
|
||||
Log.e(TAG, "網絡連接異常: " + e.message)
|
||||
errorMsg = "網絡異常,請檢查您的網絡再試~"
|
||||
errorCode = ErrorStatus.NETWORK_ERROR
|
||||
}
|
||||
is IllegalArgumentException -> {
|
||||
errorMsg = "參數錯誤"
|
||||
errorCode = ErrorStatus.SERVER_ERROR
|
||||
}
|
||||
else -> {//未知错误
|
||||
try {
|
||||
Log.e(TAG, "錯誤: " + e.message)
|
||||
} catch (e1: Exception) {
|
||||
Log.e(TAG, "未知錯誤Debug調試 ")
|
||||
}
|
||||
errorMsg = e.message.toString()
|
||||
errorCode = ErrorStatus.UNKNOWN_ERROR
|
||||
}
|
||||
}
|
||||
return errorMsg
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user