diff --git a/app/src/main/java/com/xiaoqu/watch/network/UnbindInterceptor.kt b/app/src/main/java/com/xiaoqu/watch/network/UnbindInterceptor.kt index c2743da..1b3aa62 100644 --- a/app/src/main/java/com/xiaoqu/watch/network/UnbindInterceptor.kt +++ b/app/src/main/java/com/xiaoqu/watch/network/UnbindInterceptor.kt @@ -14,6 +14,7 @@ import javax.inject.Singleton /** * 解绑拦截器 * 检测 API 返回 code=104 时,自动清除用户数据并发送解绑事件 + * 使用 peekBody 安全读取响应体,不影响后续解析 */ @Singleton class UnbindInterceptor @Inject constructor( @@ -25,23 +26,25 @@ class UnbindInterceptor @Inject constructor( override fun intercept(chain: Interceptor.Chain): Response { val response = chain.proceed(chain.request()) + // 只处理成功的 HTTP 响应 if (response.isSuccessful) { try { - // 窥视响应体(不消耗) - val source = response.body?.source() ?: return response - source.request(Long.MAX_VALUE) - val buffer = source.buffer.clone() - val json = buffer.readUtf8() + // peekBody 安全读取副本,不消耗原始 body + val peekBody = response.peekBody(1024) + val json = peekBody.string() - val apiResponse = gson.fromJson(json, ApiResponse::class.java) - if (apiResponse?.isUnbound == true) { - Timber.w("收到 code=104,执行自动解绑") - userPrefs.clear() - runBlocking { - eventBus.emit(AppEvent.DeviceUnbound) + if (json.isNotEmpty()) { + val apiResponse = gson.fromJson(json, ApiResponse::class.java) + if (apiResponse?.isUnbound == true) { + Timber.w("收到 code=104,执行自动解绑") + userPrefs.clear() + runBlocking { + eventBus.emit(AppEvent.DeviceUnbound) + } } } } catch (e: Exception) { + // 解析异常不影响正常请求 Timber.w(e, "解绑检测异常") } }