Fix:添加1s超时

This commit is contained in:
LeGend-wLw 2024-02-05 14:14:37 +08:00
parent b4abe0ccc6
commit 36eacb7a57
1 changed files with 11 additions and 9 deletions

View File

@ -19,9 +19,10 @@ object ChannelUtils {
*/ */
suspend fun getServerVersion(context: Context): Int { suspend fun getServerVersion(context: Context): Int {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
val client = okhttp3.OkHttpClient() val client = okhttp3.OkHttpClient.Builder().connectTimeout(1, java.util.concurrent.TimeUnit.SECONDS)
val request = okhttp3.Request.Builder().url(getServerVersionUrl(context)).build() .readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build()
client.newCall(request).execute().use { response -> client.newCall(okhttp3.Request.Builder().url(getServerVersionUrl(context)).build()).execute()
.use { response ->
if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response") if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response")
val body = response.body() val body = response.body()
body?.string()?.toInt() ?: 0 body?.string()?.toInt() ?: 0
@ -40,7 +41,8 @@ object ChannelUtils {
*/ */
suspend fun getServerChannel(url: String): List<TV> { suspend fun getServerChannel(url: String): List<TV> {
val result = withContext(Dispatchers.IO) { val result = withContext(Dispatchers.IO) {
val client = okhttp3.OkHttpClient() val client = okhttp3.OkHttpClient.Builder().connectTimeout(1, java.util.concurrent.TimeUnit.SECONDS)
.readTimeout(1, java.util.concurrent.TimeUnit.SECONDS).build()
val request = okhttp3.Request.Builder().url(url).build() val request = okhttp3.Request.Builder().url(url).build()
client.newCall(request).execute().use { response -> client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response") if (!response.isSuccessful) throw java.io.IOException("Unexpected code $response")
@ -49,7 +51,7 @@ object ChannelUtils {
} }
} }
return withContext(Dispatchers.Default) { return withContext(Dispatchers.Default) {
val type = object : com.google.gson.reflect.TypeToken<List <TV>>() {}.type val type = object : com.google.gson.reflect.TypeToken<List<TV>>() {}.type
com.google.gson.Gson().fromJson(result, type) com.google.gson.Gson().fromJson(result, type)
} }
} }