fix: UnbindInterceptor 改用 peekBody 防止消费响应体
source.request() 在空body/非JSON响应时会导致后续Gson解析失败。 改用 peekBody(1024) 安全读取副本。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@ import javax.inject.Singleton
|
|||||||
/**
|
/**
|
||||||
* 解绑拦截器
|
* 解绑拦截器
|
||||||
* 检测 API 返回 code=104 时,自动清除用户数据并发送解绑事件
|
* 检测 API 返回 code=104 时,自动清除用户数据并发送解绑事件
|
||||||
|
* 使用 peekBody 安全读取响应体,不影响后续解析
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
class UnbindInterceptor @Inject constructor(
|
class UnbindInterceptor @Inject constructor(
|
||||||
@@ -25,14 +26,14 @@ class UnbindInterceptor @Inject constructor(
|
|||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val response = chain.proceed(chain.request())
|
val response = chain.proceed(chain.request())
|
||||||
|
|
||||||
|
// 只处理成功的 HTTP 响应
|
||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) {
|
||||||
try {
|
try {
|
||||||
// 窥视响应体(不消耗)
|
// peekBody 安全读取副本,不消耗原始 body
|
||||||
val source = response.body?.source() ?: return response
|
val peekBody = response.peekBody(1024)
|
||||||
source.request(Long.MAX_VALUE)
|
val json = peekBody.string()
|
||||||
val buffer = source.buffer.clone()
|
|
||||||
val json = buffer.readUtf8()
|
|
||||||
|
|
||||||
|
if (json.isNotEmpty()) {
|
||||||
val apiResponse = gson.fromJson(json, ApiResponse::class.java)
|
val apiResponse = gson.fromJson(json, ApiResponse::class.java)
|
||||||
if (apiResponse?.isUnbound == true) {
|
if (apiResponse?.isUnbound == true) {
|
||||||
Timber.w("收到 code=104,执行自动解绑")
|
Timber.w("收到 code=104,执行自动解绑")
|
||||||
@@ -41,7 +42,9 @@ class UnbindInterceptor @Inject constructor(
|
|||||||
eventBus.emit(AppEvent.DeviceUnbound)
|
eventBus.emit(AppEvent.DeviceUnbound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
// 解析异常不影响正常请求
|
||||||
Timber.w(e, "解绑检测异常")
|
Timber.w(e, "解绑检测异常")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user