Ben kimlik doğrulaması için bu önleme kullanmaya çalışıyorum:android - OkHttp önleme - çoktan "tüketilen" yanıtı
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;
}
sorun benim onay (tepki süresi dolmuş belirteci gösterir) olmasıdır durumla ilgili değil, ben gerçek cevabı kontrol etmeliyim (vücut içeriği). Kontrolden sonra, yanıt "tüketilir" ve vücudu hazırlamaya yönelik herhangi bir girişim başarısız olur. Ben "klon", eskisi gibi okumak tepki tampon denedim
:
public static String responseAsString(Response response){
Buffer clonedBuffer = response.body().source().buffer().clone();
return ByteString.of(clonedBuffer.readByteArray()).toString();
}
ama çalışmıyor, clonedBuffer boş. Herhangi bir yardım takdir edilecektir.
çok teşekkürler! çözüldü :) – aw4y
Belki de MediaType contentType = ** yanıt ** .vb(). contentType() ;? –
ResponseBody.create (contentType, bodyString) için teşekkürler; – Harry