Я пытаюсь использовать этот перехватчик для проверки подлинности:андроид - 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 пуст. Любая помощь будет оценена по достоинству.
спасибо большое! resolved :) – aw4y
Возможно, MediaType contentType = ** response **. body(). contentType() ;? –
Спасибо за ResponseBody.create (contentType, bodyString); – Harry