diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 4f0ed78bd..0b83c0616 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -3,13 +3,11 @@ package io.legado.app.help.http import io.legado.app.utils.msg import kotlinx.coroutines.suspendCancellableCoroutine import okhttp3.* -import java.io.IOException import java.net.InetSocketAddress import java.net.Proxy import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.TimeUnit import kotlin.coroutines.resume -import kotlin.coroutines.resumeWithException @Suppress("unused") object HttpHelper { @@ -42,24 +40,6 @@ object HttpHelper { builder.build() } - suspend fun awaitResponse(request: Request): Response = suspendCancellableCoroutine { block -> - val call = client.newCall(request) - - block.invokeOnCancellation { - call.cancel() - } - - call.enqueue(object : Callback { - override fun onFailure(call: Call, e: IOException) { - block.resumeWithException(e) - } - - override fun onResponse(call: Call, response: Response) { - block.resume(response) - } - }) - } - /** * 缓存代理okHttp */ diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt index 4b7738308..cfcf094ad 100644 --- a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt +++ b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt @@ -1,6 +1,7 @@ package io.legado.app.lib.webdav import io.legado.app.help.http.HttpHelper +import io.legado.app.utils.await import okhttp3.* import okhttp3.MediaType.Companion.toMediaType import okhttp3.RequestBody.Companion.asRequestBody @@ -119,7 +120,7 @@ class WebDav(urlStr: String) { request.header("Authorization", Credentials.basic(it.user, it.pass)) } request.header("Depth", if (depth < 0) "infinity" else depth.toString()) - return HttpHelper.awaitResponse(request.build()) + return HttpHelper.client.newCall(request.build()).await() } return null } @@ -233,7 +234,7 @@ class WebDav(urlStr: String) { HttpAuth.auth?.let { requestBuilder.header("Authorization", Credentials.basic(it.user, it.pass)) } - val response = HttpHelper.awaitResponse(requestBuilder.build()) + val response = HttpHelper.client.newCall(requestBuilder.build()).await() return response.isSuccessful } diff --git a/app/src/main/java/io/legado/app/utils/OkHttpExtensions.kt b/app/src/main/java/io/legado/app/utils/OkHttpExtensions.kt new file mode 100644 index 000000000..b21bcc8e4 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/OkHttpExtensions.kt @@ -0,0 +1,27 @@ +package io.legado.app.utils + +import kotlinx.coroutines.suspendCancellableCoroutine +import okhttp3.Call +import okhttp3.Callback +import okhttp3.Response +import java.io.IOException +import kotlin.coroutines.resume +import kotlin.coroutines.resumeWithException + +suspend fun Call.await(): Response = suspendCancellableCoroutine { block -> + + block.invokeOnCancellation { + cancel() + } + + enqueue(object : Callback { + override fun onFailure(call: Call, e: IOException) { + block.resumeWithException(e) + } + + override fun onResponse(call: Call, response: Response) { + block.resume(response) + } + }) + +} \ No newline at end of file