android 拦截器的作用,android - OkHttp拦截器 - 响应已经"消耗"

我正在尝试使用此拦截器进行身份验证:

public class CustomInterceptor implements Interceptor {

@Override

public Response intercept(Chain chain) throws IOException {

Request request = chain.request();

// try the request

Response response = chain.proceed(request);

if (response shows expired token) {

// get a new token (I use a synchronous Retrofit call)

// create a new request and modify it accordingly using the new token

Request newRequest = request.newBuilder()...build();

// retry the request

return chain.proceed(newRequest);

}

// otherwise just pass the original response on

return response;

}

问题是我的检查(响应显示过期令牌)与状态无关,我需要检查实际响应(正文内容).因此,在检查之后,响应被"消耗"并且任何准备身体的尝试都将失败.

我试图在读取之前"克隆"响应缓冲区,如:

public static String responseAsString(Response response ){

Buffer clonedBuffer = response.body().source().buffer().clone();

return ByteString.of(clonedBuffer.readByteArray()).toString();

}

但它不起作用,clonedBuffer为空.任何帮助将不胜感激.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值