From 51645b9e8dddebb4880726e3813c90a86e4b405a Mon Sep 17 00:00:00 2001 From: liujingxing <327744707@qq.com> Date: Sat, 4 Sep 2021 23:14:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E7=89=88=E6=9C=AC=E5=8F=B7?= =?UTF-8?q?=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/rxhttp/compiler/RxJavaVersion.kt | 23 ++++++++++-- .../java/rxhttp/wrapper/OkHttpCompat.java | 35 ++++++++++++++++--- .../java/rxhttp/wrapper/utils/LogUtil.java | 3 +- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/rxhttp-compiler/src/main/java/com/rxhttp/compiler/RxJavaVersion.kt b/rxhttp-compiler/src/main/java/com/rxhttp/compiler/RxJavaVersion.kt index b87ed502..f3d86674 100644 --- a/rxhttp-compiler/src/main/java/com/rxhttp/compiler/RxJavaVersion.kt +++ b/rxhttp-compiler/src/main/java/com/rxhttp/compiler/RxJavaVersion.kt @@ -34,7 +34,7 @@ fun initRxJavaVersion(version: String?) { else -> version } ?: return rxJavaVersion = realVersion - if (realVersion >= "3.0.0") { + if (realVersion.versionCompare("3.0.0") >= 0) { rxJavaClassList["Scheduler"] = "io.reactivex.rxjava3.core" rxJavaClassList["Observable"] = "io.reactivex.rxjava3.core" rxJavaClassList["Consumer"] = "io.reactivex.rxjava3.functions" @@ -44,7 +44,7 @@ fun initRxJavaVersion(version: String?) { rxJavaClassList["Exceptions"] = "io.reactivex.rxjava3.exceptions" rxJavaClassList["Disposable"] = "io.reactivex.rxjava3.disposables" rxJavaClassList["DisposableHelper"] = "io.reactivex.rxjava3.internal.disposables" - rxJavaClassList["SpscArrayQueue"] = if (realVersion >= "3.1.1") { + rxJavaClassList["SpscArrayQueue"] = if (realVersion.versionCompare("3.1.1") >= 0) { "io.reactivex.rxjava3.operators" } else { "io.reactivex.rxjava3.internal.queue" @@ -65,4 +65,23 @@ fun initRxJavaVersion(version: String?) { rxJavaClassList["Disposable"] = "io.reactivex.disposables" rxJavaClassList["ObservableSource"] = "io.reactivex" } +} + +private fun String.versionCompare(version: String): Int { + val versionArr1 = split(".") + val versionArr2 = version.split(".") + val minLen = versionArr1.size.coerceAtMost(versionArr2.size) + var diff = 0 + for (i in 0 until minLen) { + val v1 = versionArr1[i] + val v2 = versionArr2[i] + diff = v1.length - v2.length + if (diff == 0) { + diff = v1.compareTo(v2) + } + if (diff != 0) { + break + } + } + return if (diff != 0) diff else versionArr1.size - versionArr2.size } \ No newline at end of file diff --git a/rxhttp/src/main/java/rxhttp/wrapper/OkHttpCompat.java b/rxhttp/src/main/java/rxhttp/wrapper/OkHttpCompat.java index 2771e3bf..228da9a6 100644 --- a/rxhttp/src/main/java/rxhttp/wrapper/OkHttpCompat.java +++ b/rxhttp/src/main/java/rxhttp/wrapper/OkHttpCompat.java @@ -142,8 +142,7 @@ public static long getContentLength(Response response) { //解析http状态行 public static StatusLine parse(String statusLine) throws IOException { - String okHttpUserAgent = getOkHttpUserAgent(); - if (okHttpUserAgent.compareTo("okhttp/4.0.0") >= 0) { + if (okHttpVersionCompare("4.0.0") >= 0) { return StatusLine.Companion.parse(statusLine); } else { Class statusLineClass = StatusLine.class; @@ -157,10 +156,9 @@ public static StatusLine parse(String statusLine) throws IOException { } public static DiskLruCache newDiskLruCache(FileSystem fileSystem, File directory, int appVersion, int valueCount, long maxSize) { - String okHttpVersion = getOkHttpUserAgent(); - if (okHttpVersion.compareTo("okhttp/4.3.0") >= 0) { + if (okHttpVersionCompare("4.3.0") >= 0) { return new DiskLruCache(fileSystem, directory, appVersion, valueCount, maxSize, TaskRunner.INSTANCE); - } else if (okHttpVersion.compareTo("okhttp/4.0.0") >= 0) { + } else if (okHttpVersionCompare("4.0.0") >= 0) { Companion companion = DiskLruCache.Companion; Class clazz = companion.getClass(); try { @@ -181,6 +179,13 @@ public static DiskLruCache newDiskLruCache(FileSystem fileSystem, File directory throw new RuntimeException("Please upgrade OkHttp to V3.12.0 or higher"); } + //okhttp版本比较,当前版本大于version2,返回 >0; 等于,返回=0; 否则,返回 <0 + public static int okHttpVersionCompare(String version2) { + String[] okHttpUserAgentArr = getOkHttpUserAgent().split("/"); + String okhttpVersion = okHttpUserAgentArr[okHttpUserAgentArr.length - 1]; + return versionCompare(okhttpVersion, version2); + } + //获取OkHttp版本号 public static String getOkHttpUserAgent() { if (OKHTTP_USER_AGENT != null) return OKHTTP_USER_AGENT; @@ -206,4 +211,24 @@ public static String getOkHttpUserAgent() { } return OKHTTP_USER_AGENT = "okhttp/4.2.0"; } + + private static int versionCompare(String version1, String version2) { + String[] versionArr1 = version1.split("\\."); + String[] versionArr2 = version2.split("\\."); + int minLen = Math.min(versionArr1.length, versionArr2.length); + int diff = 0; + for (int i = 0; i < minLen; i++) { + String v1 = versionArr1[i]; + String v2 = versionArr2[i]; + diff = v1.length() - v2.length(); + if (diff == 0) { + diff = v1.compareTo(v2); + } + if (diff != 0) { + break; + } + } + diff = (diff != 0) ? diff : (versionArr1.length - versionArr2.length); + return diff; + } } diff --git a/rxhttp/src/main/java/rxhttp/wrapper/utils/LogUtil.java b/rxhttp/src/main/java/rxhttp/wrapper/utils/LogUtil.java index 177b64fc..00f11a82 100644 --- a/rxhttp/src/main/java/rxhttp/wrapper/utils/LogUtil.java +++ b/rxhttp/src/main/java/rxhttp/wrapper/utils/LogUtil.java @@ -279,8 +279,7 @@ private static String multipartBody2Str(MultipartBody multipartBody) { } private static boolean versionGte3140() { - String okHttpVersion = OkHttpCompat.getOkHttpUserAgent(); - return okHttpVersion.compareTo("okhttp/3.14.0") >= 0; + return OkHttpCompat.okHttpVersionCompare("3.14.0") >= 0; } @SuppressWarnings("deprecation")