refactor(net): 优化API请求错误处理和基础组件功能

- 添加JsonSyntaxException异常处理,提供更友好的错误提示
- 修复API响应判断逻辑,移除不必要的空值检查
- 优化HttpException转换为ApiException时的数据解析
- 移除BaseActivity中废弃的launch方法和相关变量
- 重构BaseActivity中的权限请求和页面跳转回调机制
- 更新Loading对话框的显示方法,支持自定义消息和内容更新
- 修复CollectFragment中的语法错误和日志记录功能
- 优化DishSamplingActivity中的页面跳转实现方式
- 完善SettingActivity中的相机权限检查逻辑
This commit is contained in:
2026-03-30 15:28:52 +08:00
parent 28c7628321
commit d6a9c3562d
6 changed files with 147 additions and 83 deletions
@@ -1,5 +1,6 @@
package com.shuwei.dish.match.net
import com.google.gson.JsonSyntaxException
import com.shuwei.dish.match.utils.ext.toJsonString
import com.shuwei.dish.match.utils.ext.toObject
import com.shuwei.dish.match.utils.ext.toType
@@ -60,10 +61,10 @@ suspend fun <T> request(
onFailure("-1", apiException.errorMsg)
return
}
if (response?.isSuccess() == true) {
if (response.isSuccess()) {
onSuccess(response.data as T)
} else {
onFailure(response?.code ?: "-1", response?.msg ?: "")
onFailure(response.code, response.msg ?: "")
}
}
@@ -77,6 +78,11 @@ fun getApiException(e: Exception): ApiException {
return when (e) {
is HttpException -> httpException2ApiException(e)
is JsonSyntaxException -> ApiException(
ErrorType.NETWORK_ERROR,
errorMsg = "服务器响应异常,请稍后重试"
)
is UnknownHostException -> ApiException(
ErrorType.NETWORK_ERROR,
errorMsg = "网络未连接,请检查网络"
@@ -108,10 +114,15 @@ fun getApiException(e: Exception): ApiException {
fun httpException2ApiException(e: HttpException): ApiException {
val body = e.response()?.errorBody()?.string()
val codeMsg: CodeMsg? = body?.toObject<CodeMsg>()
val codeMsg: CodeMsg? = try {
body?.toObject<CodeMsg>()
} catch (ex: Exception) {
null
}
return ApiException(
ErrorType.NETWORK_ERROR,
errorMsg = codeMsg?.msg ?: ""
errorMsg = if (codeMsg == null) "数据解析异常(${e.code()})"
else codeMsg.msg ?: "服务器错误(${e.code()})"
)
}