diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index fc2fc92ff..7a38e2a33 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -30,12 +30,13 @@ interface JsExtensions { */ fun ajax(urlStr: String): String? { return runBlocking { - try { + kotlin.runCatching { val analyzeUrl = AnalyzeUrl(urlStr) analyzeUrl.getStrResponse(urlStr).body - } catch (e: Exception) { - e.printStackTrace() - e.msg + }.onFailure { + it.printStackTrace() + }.getOrElse { + it.msg } } } @@ -44,13 +45,15 @@ interface JsExtensions { * 访问网络,返回Response */ fun connect(urlStr: String): Any { - return try { - val analyzeUrl = AnalyzeUrl(urlStr) - runBlocking { + return runBlocking { + kotlin.runCatching { + val analyzeUrl = AnalyzeUrl(urlStr) analyzeUrl.getStrResponse(urlStr) + }.onFailure { + it.printStackTrace() + }.getOrElse { + it.msg } - } catch (e: Exception) { - e.msg } } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 6fc8e2146..a1d75941c 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -62,10 +62,10 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { fun setBaseUrl(baseUrl: String?): AnalyzeRule { baseUrl?.let { this.baseUrl = baseUrl - try { + kotlin.runCatching { baseURL = URL(baseUrl.substringBefore(",")) - } catch (e: Exception) { - e.printStackTrace() + }.onFailure { + it.printStackTrace() } } return this @@ -229,11 +229,9 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { } } if (result == null) result = "" - val str = try { + val str = kotlin.runCatching { Entities.unescape(result.toString()) - } catch (e: Exception) { - result.toString() - } + }.getOrDefault(result.toString()) if (isUrl) { return if (str.isBlank()) { baseUrl ?: "" @@ -654,12 +652,13 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { */ override fun ajax(urlStr: String): String? { return runBlocking { - try { + kotlin.runCatching { val analyzeUrl = AnalyzeUrl(urlStr, book = book) analyzeUrl.getStrResponse(urlStr).body - } catch (e: Exception) { - e.printStackTrace() - e.msg + }.onFailure { + it.printStackTrace() + }.getOrElse { + it.msg } } } diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index d895def23..805fdfc14 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -120,7 +120,7 @@ class AudioPlayService : BaseService(), private fun play() { upNotification() if (requestFocus()) { - try { + kotlin.runCatching { AudioPlay.status = Status.STOP postEvent(EventBus.AUDIO_STATE, Status.STOP) mediaPlayer.reset() @@ -130,10 +130,10 @@ class AudioPlayService : BaseService(), mediaPlayer.setDataSource(this, uri, analyzeUrl.headerMap) mediaPlayer.prepareAsync() handler.removeCallbacks(mpRunnable) - } catch (e: Exception) { - e.printStackTrace() + }.onFailure { + it.printStackTrace() launch { - toast("$url ${e.localizedMessage}") + toast("$url ${it.localizedMessage}") stopSelf() } }