@Override public boolean fromDiskCache() { return mResponse.cacheResponse() != null; }
private static String responseSourceHeader(Response response) { if (response.networkResponse() == null) { if (response.cacheResponse() == null) { return "NONE"; } return "CACHE " + response.code(); } if (response.cacheResponse() == null) { return "NETWORK " + response.code(); } return "CONDITIONAL_CACHE " + response.networkResponse().code(); }
public void run() throws Exception { Request request = new Request.Builder() .url("http://publicobject.com/helloworld.txt") .build(); String response1Body; try (Response response1 = client.newCall(request).execute()) { if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1); response1Body = response1.body().string(); System.out.println("Response 1 response: " + response1); System.out.println("Response 1 cache response: " + response1.cacheResponse()); System.out.println("Response 1 network response: " + response1.networkResponse()); } String response2Body; try (Response response2 = client.newCall(request).execute()) { if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2); response2Body = response2.body().string(); System.out.println("Response 2 response: " + response2); System.out.println("Response 2 cache response: " + response2.cacheResponse()); System.out.println("Response 2 network response: " + response2.networkResponse()); } System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body)); }
private List<InetAddress> readResponse(String hostname, Response response) throws Exception { if (response.cacheResponse() == null && response.protocol() != Protocol.HTTP_2) { Platform.get().log(Platform.WARN, "Incorrect protocol: " + response.protocol(), null); } try { if (!response.isSuccessful()) { throw new IOException("response: " + response.code() + " " + response.message()); } ResponseBody body = response.body(); if (body.contentLength() > MAX_RESPONSE_SIZE) { throw new IOException("response size exceeds limit (" + MAX_RESPONSE_SIZE + " bytes): " + body.contentLength() + " bytes"); } ByteString responseBytes = body.source().readByteString(); return DnsRecordCodec.decodeAnswers(hostname, responseBytes); } finally { response.close(); } }
@Override public void onResponse(Call call, Response response) { if (!response.isSuccessful()) { callback.onError(new ResponseException(response.code(), request.networkPolicy)); return; } // Cache response is only null when the response comes fully from the network. Both // completely cached and conditionally cached responses will have a non-null cache response. Picasso.LoadedFrom loadedFrom = response.cacheResponse() == null ? NETWORK : DISK; // Sometimes response content length is zero when requests are being replayed. Haven't found // root cause to this but retrying the request seems safe to do so. ResponseBody body = response.body(); if (loadedFrom == DISK && body.contentLength() == 0) { body.close(); callback.onError( new ContentLengthException("Received response with 0 content-length header.")); return; } if (loadedFrom == NETWORK && body.contentLength() > 0) { stats.dispatchDownloadFinished(body.contentLength()); } try { Bitmap bitmap = decodeStream(body.source(), request); callback.onSuccess(new Result(bitmap, loadedFrom)); } catch (IOException e) { body.close(); callback.onError(e); } }
Utils.saveFile(okHttpResponse, request.getDirPath(), request.getFileName()); final long timeTaken = System.currentTimeMillis() - startTime; if (okHttpResponse.cacheResponse() == null) { final long finalBytes = TrafficStats.getTotalRxBytes(); final long diffBytes;
final long timeTaken = System.currentTimeMillis() - startTime; if (request.getAnalyticsListener() != null) { if (okHttpResponse.cacheResponse() == null) { Utils.sendAnalytics(request.getAnalyticsListener(), timeTaken, requestBodyLength, okHttpResponse.body().contentLength(), false);
@SuppressWarnings("unchecked") InterceptorResponse parse(Operation operation, okhttp3.Response httpResponse) throws ApolloHttpException, ApolloParseException { String cacheKey = httpResponse.request().header(HttpCache.CACHE_KEY_HEADER); if (httpResponse.isSuccessful()) { try { OperationResponseParser parser = new OperationResponseParser(operation, responseFieldMapper, scalarTypeAdapters, normalizer); Response parsedResponse = parser.parse(httpResponse.body().source()) .toBuilder() .fromCache(httpResponse.cacheResponse() != null) .build(); if (parsedResponse.hasErrors() && httpCache != null) { httpCache.removeQuietly(cacheKey); } return new InterceptorResponse(httpResponse, parsedResponse, normalizer.records()); } catch (Exception rethrown) { logger.e(rethrown, "Failed to parse network response for operation: %s", operation); closeQuietly(httpResponse); if (httpCache != null) { httpCache.removeQuietly(cacheKey); } throw new ApolloParseException("Failed to parse http response", rethrown); } } else { logger.e("Failed to parse network response: %s", httpResponse); throw new ApolloHttpException(httpResponse); } }
Utils.saveFile(okHttpResponse, request.getDirPath(), request.getFileName()); final long timeTaken = System.currentTimeMillis() - startTime; if (okHttpResponse.cacheResponse() == null) { final long finalBytes = TrafficStats.getTotalRxBytes(); final long diffBytes;
okHttpResponse = request.getCall().execute(); final long timeTaken = System.currentTimeMillis() - startTime; if (okHttpResponse.cacheResponse() == null) { final long finalBytes = TrafficStats.getTotalRxBytes(); final long diffBytes;
final long timeTaken = System.currentTimeMillis() - startTime; if (request.getAnalyticsListener() != null) { if (okHttpResponse.cacheResponse() == null) { Utils.sendAnalytics(request.getAnalyticsListener(), timeTaken, requestBodyLength, okHttpResponse.body().contentLength(), false);
Utils.saveFile(okHttpResponse, request.getDirPath(), request.getFileName()); final long timeTaken = System.currentTimeMillis() - startTime; if (okHttpResponse.cacheResponse() == null) { final long finalBytes = TrafficStats.getTotalRxBytes(); final long diffBytes;
final long timeTaken = System.currentTimeMillis() - startTime; if (request.getAnalyticsListener() != null) { if (okHttpResponse.cacheResponse() == null) { Utils.sendAnalytics(request.getAnalyticsListener(), timeTaken, requestBodyLength, okHttpResponse.body().contentLength(), false);
okHttpResponse = call.execute(); final long timeTaken = System.currentTimeMillis() - startTime; if (okHttpResponse.cacheResponse() == null) { final long finalBytes = TrafficStats.getTotalRxBytes(); final long diffBytes;
okHttpResponse = call.execute(); final long timeTaken = System.currentTimeMillis() - startTime; if (okHttpResponse.cacheResponse() == null) { final long finalBytes = TrafficStats.getTotalRxBytes(); final long diffBytes;
boolean fromCache = response.cacheResponse() != null;
private static String responseSourceHeader(Response response) { if (response.networkResponse() == null) { if (response.cacheResponse() == null) { return "NONE"; } return "CACHE " + response.code(); } if (response.cacheResponse() == null) { return "NETWORK " + response.code(); } return "CONDITIONAL_CACHE " + response.networkResponse().code(); }
@Override public void onResponse(Call call, Response response) throws IOException { if (null != response.cacheResponse()) { String str = response.cacheResponse().toString(); Log.d(TAG, "cache---" + str); } else { String str = response.networkResponse().toString(); Log.d(TAG, "network---" + str); } } });
@Override public void onResponse(Call call, Response response) throws IOException { Log.e(TAG, "onResponse1: body==" + response.body().string()); Log.e(TAG, "onResponse1: netResponse==" + response.networkResponse()); Log.e(TAG, "onResponse1: cacheResponse==" + response.cacheResponse()); Log.e(TAG, "onResponse1: cacheControl==" + response.cacheControl().toString()); Log.e(TAG, "onResponse1: threadName==" + Thread.currentThread().getName()); } });
@Override public void onResponse(Call call, Response response) throws IOException { Log.e(TAG, "onResponse: body==" + response.body().string()); Log.e(TAG, "onResponse: netResponse==" + response.networkResponse()); Log.e(TAG, "onResponse: cacheResponse==" + response.cacheResponse()); Log.e(TAG, "onResponse: cacheControl==" + response.cacheControl().toString()); Log.e(TAG, "onResponse: threadName==" + Thread.currentThread().getName()); } });