From 6ac33616deecc4a2c5dd2ac75fbb30d32a9c0d3c Mon Sep 17 00:00:00 2001 From: Hu Shenghao Date: Fri, 1 Sep 2023 17:01:30 +0800 Subject: [PATCH] chore: replace OkHttp to Java HttpClient --- build.gradle.kts | 3 +- buildSrc/build.gradle.kts | 1 - .../com/dede/easter_eggs/EmojiSvg2XmlTask.kt | 37 +++++++++---------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f89e69ef..6415dcc9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,3 @@ -@file:Suppress("UnstableApiUsage") - import com.android.build.gradle.AppExtension import com.android.build.gradle.AppPlugin import com.android.build.gradle.BaseExtension @@ -33,6 +31,7 @@ fun Project.android(configure: Action? = null) { if (kotlinOptions != null) { kotlinOptions.jvmTarget = Versions.JAVA_VERSION.toString() } + @Suppress("UNCHECKED_CAST") configure?.execute(this as T) } } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index eefac586..f9acf7de 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -11,5 +11,4 @@ repositories { dependencies { implementation(gradleApi()) implementation("com.android.tools:sdk-common:31.1.1") - implementation("com.squareup.okhttp3:okhttp:4.11.0") } diff --git a/buildSrc/src/main/java/com/dede/easter_eggs/EmojiSvg2XmlTask.kt b/buildSrc/src/main/java/com/dede/easter_eggs/EmojiSvg2XmlTask.kt index ccdc1f6c..043835a3 100644 --- a/buildSrc/src/main/java/com/dede/easter_eggs/EmojiSvg2XmlTask.kt +++ b/buildSrc/src/main/java/com/dede/easter_eggs/EmojiSvg2XmlTask.kt @@ -1,13 +1,16 @@ package com.dede.easter_eggs import com.android.ide.common.vectordrawable.Svg2Vector -import okhttp3.OkHttpClient -import okhttp3.Request import org.gradle.api.DefaultTask import org.gradle.api.tasks.Input import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction import java.io.File +import java.net.URI +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.net.http.HttpResponse.BodyHandlers /** * Download emoji svg and convert to android drawable xml. @@ -60,7 +63,7 @@ open class EmojiSvg2XmlTask : DefaultTask() { "🐢", "✨", "🌟", "👑"// ) - private lateinit var httpClient: OkHttpClient + private lateinit var httpClient: HttpClient init { if (!::svgOutputDir.isInitialized) { @@ -69,7 +72,9 @@ open class EmojiSvg2XmlTask : DefaultTask() { } private fun prepare() { - httpClient = OkHttpClient.Builder().build() + httpClient = HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.NORMAL) + .build() if (!svgOutputDir.exists()) { svgOutputDir.mkdirs() } @@ -118,27 +123,19 @@ open class EmojiSvg2XmlTask : DefaultTask() { postfix = ".svg" ).toString() val url = EMOJI_SVG_URL.format(svgFileName) + val svgFile = File(svgOutputDir, svgFileName) val request = createHttpRequest(url) - val response = httpClient.newCall(request).execute() - val responseBody = response.body - if (response.code != 200 || responseBody == null) { - println("Download emoji svg failure: ${response.code} -> $url") + val response = httpClient.send(request, BodyHandlers.ofFile(svgFile.toPath())) + if (response.statusCode() != 200) { + println("Download emoji svg failure: ${response.statusCode()} -> $url") return null } - - val svgFile = File(svgOutputDir, svgFileName) - responseBody.byteStream().use { input -> - svgFile.outputStream().use { - input.copyTo(it) - } - } - return svgFile + return response.body().toFile() } - private fun createHttpRequest(url: String): Request { - return Request.Builder() - .url(url) - .get() + private fun createHttpRequest(url: String): HttpRequest { + return HttpRequest.newBuilder(URI.create(url)) + .GET() .build() }