fixed host2 download issues.
authorTrillGates <williamtrillgates@gmail.com>
Tue, 20 Jul 2021 14:10:07 +0000 (22:10 +0800)
committerTrillGates <williamtrillgates@gmail.com>
Tue, 20 Jul 2021 14:10:07 +0000 (22:10 +0800)
app/src/main/java/com/ic/icfotaclient/utils/ICOtaApiUtils.kt

index 83abc5f..e802d10 100644 (file)
@@ -97,35 +97,49 @@ object ICOtaApiUtils {
         if (okHttpClient == null) {
             initOkHttp()
         }
-        val request: Request = Request.Builder().url(url).build()
-        val newCall = okHttpClient?.newCall(request)
-        val response = newCall?.execute()
-        Log.d(TAG, "execute == > $response")
-        //解析数据
-        val code = response?.code()
-        Log.d(TAG, "code == > $code")
-        if (response != null && code == HttpURLConnection.HTTP_OK) {
-            val body = response.body() ?: return null
-            val json = body.string()
-            Log.d(TAG, "result == > $json")
-            val gson = Gson()
-            otaInfo = gson.fromJson(json, ICOtaInfo::class.java)
-            if (otaInfo != null) {
-                if (otaInfo!!.UptoDate) {
-                    Log.d(TAG, "no update package in ic server...$isBackUpHost")
-                    if (!isBackUpHost) {
-                        isBackUpHost = true
-                        return getUpdateInfo(backupUrl)
+        try {
+            val request: Request = Request.Builder().url(url).build()
+            val newCall = okHttpClient?.newCall(request)
+            val response = newCall?.execute()
+            Log.d(TAG, "execute == > $response")
+            //解析数据
+            val code = response?.code()
+            Log.d(TAG, "code == > $code")
+            if (response != null && code == HttpURLConnection.HTTP_OK) {
+                val body = response.body() ?: return null
+                val json = body.string()
+                Log.d(TAG, "result == > $json")
+                val gson = Gson()
+                otaInfo = gson.fromJson(json, ICOtaInfo::class.java)
+                if (otaInfo != null) {
+                    if (otaInfo!!.UptoDate) {
+                        Log.d(TAG, "no update package in ic server...$isBackUpHost")
+                        if (!isBackUpHost) {
+                            isBackUpHost = true
+                            return getUpdateInfo(backupUrl)
+                        }
+                        //备份的服务器都走完了,还是没有升级包,就直接返回null
+                        return null
+                    } else {
+                        //需要升级的,返回数据
+                        return OtaInfo(
+                            otaInfo!!.AutoUpdate,
+                            otaInfo!!.description,
+                            otaInfo!!.OtaPackageVersion,
+                            otaInfo!!.OtaPackageLength.toLong(),
+                            System.currentTimeMillis(),
+                            OtaInfo.ServerInc.IN_CAR
+                        )
                     }
                 }
-                return OtaInfo(
-                    otaInfo!!.AutoUpdate,
-                    otaInfo!!.description,
-                    otaInfo!!.OtaPackageVersion,
-                    otaInfo!!.OtaPackageLength.toLong(),
-                    System.currentTimeMillis(),
-                    OtaInfo.ServerInc.IN_CAR
-                )
+            }
+        } catch (e: Exception) {
+            e.printStackTrace()
+            Log.d(TAG, "exception....$e")
+            //如果访问自己的出错了,再访问备用的
+            if (!isBackUpHost) {
+                isBackUpHost = true
+                return getUpdateInfo(backupUrl)
             }
         }
         Log.d(TAG, "no update info...")