use of com.android.volley.NetworkResponse in project platform_frameworks_base by android.
the class URLFetcher method getExpirationTimeMillisFromHTTPHeader.
/**
* Parses the HTTP headers to compute the ttl.
*
* @param headers a map that map the header key to the header values. Can be null.
* @return the ttl in millisecond or null if the ttl is not specified in the header.
*/
private Long getExpirationTimeMillisFromHTTPHeader(Map<String, List<String>> headers) {
if (headers == null) {
return null;
}
Map<String, String> joinedHeaders = joinHttpHeaders(headers);
NetworkResponse response = new NetworkResponse(null, joinedHeaders);
Cache.Entry cachePolicy = HttpHeaderParser.parseCacheHeaders(response);
if (cachePolicy == null) {
// Cache is disabled, set the expire time to 0.
return DO_NOT_CACHE_RESULT;
} else if (cachePolicy.ttl == 0) {
// Cache policy is not specified, set the expire time to 0.
return DO_NOT_CACHE_RESULT;
} else {
// cachePolicy.ttl is actually the expire timestamp in millisecond.
return cachePolicy.ttl;
}
}
use of com.android.volley.NetworkResponse in project Douya by DreaminginCodeZH.
the class BasicNetwork method performRequest.
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
long requestStart = SystemClock.elapsedRealtime();
while (true) {
HttpResponse httpResponse = null;
byte[] responseContents = null;
Map<String, String> responseHeaders = Collections.emptyMap();
try {
// Gather headers.
Map<String, String> headers = new HashMap<>();
addCacheHeaders(headers, request.getCacheEntry());
httpResponse = mHttpStack.performRequest(request, headers);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
responseHeaders = convertHeaders(httpResponse.getAllHeaders());
// Handle cache validation.
if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
Cache.Entry entry = request.getCacheEntry();
if (entry == null) {
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null, responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
}
// A HTTP 304 response does not have all header fields. We
// have to use the header fields from the cache entry plus
// the new ones from the response.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
entry.responseHeaders.putAll(responseHeaders);
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, entry.data, entry.responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
}
// Some responses such as 204s do not have content. We must check.
if (httpResponse.getEntity() != null) {
responseContents = entityToBytes(httpResponse.getEntity());
} else {
// Add 0 byte response as a way of honestly representing a
// no-content request.
responseContents = new byte[0];
}
// if the request is slow, log it.
long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
logSlowRequests(requestLifetime, request, responseContents, statusLine);
if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}
return new NetworkResponse(statusCode, responseContents, responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
} catch (MalformedURLException e) {
throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (SocketTimeoutException e) {
attemptRetryOnException("socket-timeout", request, new TimeoutError());
} catch (ConnectTimeoutException e) {
attemptRetryOnException("connection-timeout", request, new TimeoutError());
} catch (IOException e) {
int statusCode;
if (httpResponse != null) {
statusCode = httpResponse.getStatusLine().getStatusCode();
} else {
// PATCH: Let RetryPolicy decide whether the request should be aborted.
attemptRetryOnException("no-connection", request, new NoConnectionError(e));
continue;
}
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
if (responseContents != null) {
NetworkResponse networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
} else {
// PATCH: Let RetryPolicy decide whether the request should be aborted.
// TODO: Only throw ServerError for 5xx status codes.
attemptRetryOnException("server", request, new ServerError(networkResponse));
}
} else {
// PATCH: Let RetryPolicy decide whether the request should be aborted.
attemptRetryOnException("network", request, new NetworkError());
}
}
}
}
use of com.android.volley.NetworkResponse in project WordPress-Android by wordpress-mobile.
the class RestClientCustomizableMock method forgeVolleyErrorFromFilename.
private VolleyError forgeVolleyErrorFromFilename(String filename) {
String strData = fileToString(filename);
byte[] data = new byte[0];
if (strData != null) {
data = strData.getBytes();
}
NetworkResponse networkResponse = new NetworkResponse(400, data, null, false);
VolleyError ve = new VolleyError(networkResponse);
return ve;
}
use of com.android.volley.NetworkResponse in project WordPress-Android by wordpress-mobile.
the class NotificationsUpdateService method logVolleyErrorDetails.
private static void logVolleyErrorDetails(final VolleyError volleyError) {
if (volleyError == null) {
AppLog.e(AppLog.T.NOTIFS, "Tried to log a VolleyError, but the error obj was null!");
return;
}
if (volleyError.networkResponse != null) {
NetworkResponse networkResponse = volleyError.networkResponse;
AppLog.e(AppLog.T.NOTIFS, "Network status code: " + networkResponse.statusCode);
if (networkResponse.data != null) {
AppLog.e(AppLog.T.NOTIFS, "Network data: " + new String(networkResponse.data));
}
}
AppLog.e(AppLog.T.NOTIFS, "Volley Error Message: " + volleyError.getMessage(), volleyError);
}
use of com.android.volley.NetworkResponse in project TaEmCasa by Dionen.
the class HttpHeaderParserTest method setUp.
@Before
public void setUp() throws Exception {
headers = new HashMap<String, String>();
response = new NetworkResponse(0, null, headers, false);
}
Aggregations