diff --git a/README.md b/README.md index afc9f27..1608d30 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ # 依赖版本 | Dependency | Version | |--|--| -| kotlin | 1.4.21 | -| kotlinx-coroutines-android | 1.4.2 | +| kotlin | 1.4.32 | +| kotlinx-coroutines-android | 1.4.3 | | retrofit | 2.9.0 | # Gradle 依赖 @@ -26,7 +26,7 @@ allprojects { ```kotlin dependencies { - implementation 'com.github.chenxyu:retrofit-adapter-flow:1.1.3' + implementation 'com.github.chenxyu:retrofit-adapter-flow:1.1.4' } ``` diff --git a/adapter-flow/build.gradle b/adapter-flow/build.gradle index 19f91b0..d0e71c8 100644 --- a/adapter-flow/build.gradle +++ b/adapter-flow/build.gradle @@ -10,8 +10,8 @@ android { defaultConfig { minSdkVersion 21 targetSdkVersion 30 - versionCode 10 - versionName "1.1.3" + versionCode 11 + versionName "1.1.4" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles 'consumer-rules.pro' diff --git a/adapter-flow/src/main/java/com/chenxyu/retrofit/adapter/FlowCallAdapter.kt b/adapter-flow/src/main/java/com/chenxyu/retrofit/adapter/FlowCallAdapter.kt index e56c91f..cda46bb 100644 --- a/adapter-flow/src/main/java/com/chenxyu/retrofit/adapter/FlowCallAdapter.kt +++ b/adapter-flow/src/main/java/com/chenxyu/retrofit/adapter/FlowCallAdapter.kt @@ -4,6 +4,7 @@ import kotlinx.coroutines.CancellationException import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.cancel import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.channels.sendBlocking import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow import retrofit2.Call @@ -34,8 +35,12 @@ internal class FlowCallAdapter(private val responseType: Type) : if (body == null || response.code() == 204) { cancel(CancellationException("HTTP status code: ${response.code()}")) } else { - offer(body) - channel.close() + try { + sendBlocking(body) + close() + } catch (e: Exception) { + cancel(CancellationException(e.localizedMessage, e)) + } } } else { cancel(CancellationException(errorMsg(response) ?: "unknown error")) diff --git a/build.gradle b/build.gradle index 3784a48..a46a957 100644 --- a/build.gradle +++ b/build.gradle @@ -1,8 +1,8 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.4.21' - ext.kotlinx_coroutines_version = '1.4.2' + ext.kotlin_version = '1.4.32' + ext.kotlinx_coroutines_version = '1.4.3' repositories { google() jcenter()