Я пытаюсь использовать этот перехватчик для аутентификации:
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 пуст. Любая помощь будет оценена.