fix 更新版本可能导致异常崩溃

This commit is contained in:
gallonyin
2022-11-03 19:26:29 +08:00
parent 4eee19995a
commit 477fa194cb

View File

@@ -19,36 +19,40 @@ object UpdateUtil {
OkGo.get<String>(Constant.getCheckUpdateUrl()) OkGo.get<String>(Constant.getCheckUpdateUrl())
.execute(object : StringCallback() { .execute(object : StringCallback() {
override fun onSuccess(response: Response<String>) { override fun onSuccess(response: Response<String>) {
val commonResult = try {
GsonUtils.fromJson( val commonResult =
response.body(), GsonUtils.fromJson(
CheckUpdateResult::class.java response.body(),
) CheckUpdateResult::class.java
if (commonResult.code != 200) { )
return onError(response) if (commonResult.code != 200) {
} return onError(response)
LogUtils.i(commonResult.data)
commonResult.data?.apply {
if (AppUtils.getAppVersionCode() < this.versionCode) {
UpdateAppUtils
.getInstance()
.apkUrl(this.downloadUrl)
.updateTitle(this.title)
.updateContent(this.updateLog.replace("\\n", "\n"))
.updateConfig(
UpdateConfig(
force = AppUtils.getAppVersionCode() < this.minVersionCode,
serverVersionName = this.versionName,
serverVersionCode = this.versionCode
)
)
.update()
} else {
ToastUtils.showShort(R.string.update_no_update)
} }
return LogUtils.i(commonResult.data)
commonResult.data?.apply {
if (AppUtils.getAppVersionCode() < this.versionCode) {
UpdateAppUtils
.getInstance()
.apkUrl(this.downloadUrl)
.updateTitle(this.title)
.updateContent(this.updateLog.replace("\\n", "\n"))
.updateConfig(
UpdateConfig(
force = AppUtils.getAppVersionCode() < this.minVersionCode,
serverVersionName = this.versionName,
serverVersionCode = this.versionCode
)
)
.update()
} else {
ToastUtils.showShort(R.string.update_no_update)
}
return
}
} catch (e: Exception) {
LogUtils.e(e)
onError(response)
} }
ToastUtils.showLong(R.string.update_failed)
} }
override fun onError(response: Response<String>) { override fun onError(response: Response<String>) {