Skip to content

Commit

Permalink
chore: replace OkHttp to Java HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
hushenghao committed Sep 1, 2023
1 parent 6a4458b commit 6ac3361
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,6 +31,7 @@ fun <T : BaseExtension> Project.android(configure: Action<T>? = null) {
if (kotlinOptions != null) {
kotlinOptions.jvmTarget = Versions.JAVA_VERSION.toString()
}
@Suppress("UNCHECKED_CAST")
configure?.execute(this as T)
}
}
Expand Down
1 change: 0 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
37 changes: 17 additions & 20 deletions buildSrc/src/main/java/com/dede/easter_eggs/EmojiSvg2XmlTask.kt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -60,7 +63,7 @@ open class EmojiSvg2XmlTask : DefaultTask() {
"🐢", "", "🌟", "👑"//
)

private lateinit var httpClient: OkHttpClient
private lateinit var httpClient: HttpClient

init {
if (!::svgOutputDir.isInitialized) {
Expand All @@ -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()
}
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit 6ac3361

Please sign in to comment.