use of com.android.volley.NetworkResponse in project android_frameworks_base by crdroidandroid.
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 iosched by google.
the class BasicNetworkTest method testHeadersAndPostParams.
public void testHeadersAndPostParams() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
fakeResponse.setEntity(new StringEntity("foobar"));
mockHttpStack.setResponseToReturn(fakeResponse);
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) {
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
return null;
}
@Override
protected void deliverResponse(String response) {
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestheader", "foo");
return result;
}
@Override
public Map<String, String> getParams() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestpost", "foo");
return result;
}
};
httpNetwork.performRequest(request);
assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
}
use of com.android.volley.NetworkResponse in project iosched by google.
the class ImageRequestTest method testParseNetworkResponse_resizing.
public void testParseNetworkResponse_resizing() throws Exception {
byte[] jpegBytes = readRawResource(getInstrumentation().getContext().getResources(), R.raw.large_jpeg_1024_500);
NetworkResponse jpeg = new NetworkResponse(jpegBytes);
// Exact sizes
// exactly half
verifyResize(jpeg, 512, 250, 512, 250);
// just under half
verifyResize(jpeg, 511, 249, 509, 249);
// larger
verifyResize(jpeg, 1080, 500, 1024, 500);
// keep same ratio
verifyResize(jpeg, 500, 500, 500, 244);
// Specify only width, preserve aspect ratio
verifyResize(jpeg, 512, 0, 512, 250);
verifyResize(jpeg, 800, 0, 800, 390);
verifyResize(jpeg, 1024, 0, 1024, 500);
// Specify only height, preserve aspect ratio
verifyResize(jpeg, 0, 250, 512, 250);
verifyResize(jpeg, 0, 391, 800, 391);
verifyResize(jpeg, 0, 500, 1024, 500);
// No resize
verifyResize(jpeg, 0, 0, 1024, 500);
}
use of com.android.volley.NetworkResponse in project OkVolley by googolmo.
the class OkNetwork method performRequest.
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
long requestStart = SystemClock.elapsedRealtime();
while (true) {
Response httpResponse = null;
byte[] responseContents = null;
Map<String, String> responseHeaders = Collections.emptyMap();
try {
// Gather headers.
Map<String, String> headers = new HashMap<String, String>();
addCacheHeaders(headers, request.getCacheEntry());
httpResponse = mHttpStack.performRequest(request, headers);
int statusCode = httpResponse.code();
responseHeaders = new TreeMap<String, String>();
for (String field : httpResponse.headers().names()) {
responseHeaders.put(field, httpResponse.headers().get(field));
}
// Handle cache validation.
if (statusCode == 304) {
return new NetworkResponse(304, request.getCacheEntry().data, responseHeaders, true);
}
if (httpResponse.body() != null) {
if (responseGzip(responseHeaders)) {
Buffer buffer = new Buffer();
GzipSource gzipSource = new GzipSource(httpResponse.body().source());
while (gzipSource.read(buffer, Integer.MAX_VALUE) != -1) {
}
responseContents = buffer.readByteArray();
} else {
responseContents = httpResponse.body().bytes();
}
} else {
responseContents = new byte[0];
}
// // Some responses such as 204s do not have content. We must check.
// if (httpResponse.getEntity() != null) {
// responseContents = entityToBytes(httpResponse.getEntity()
// , responseGzip(responseHeaders));
// } 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, httpResponse);
if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}
return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
} catch (SocketTimeoutException e) {
attemptRetryOnException("socket", request, new TimeoutError());
} catch (ConnectTimeoutException e) {
attemptRetryOnException("connection", request, new TimeoutError());
} catch (MalformedURLException e) {
throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (IOException e) {
int statusCode;
NetworkResponse networkResponse = null;
if (httpResponse != null) {
statusCode = httpResponse.code();
} else {
throw new NoConnectionError(e);
}
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
if (responseContents != null) {
networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false);
if (statusCode == 401 || statusCode == 403) {
attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
} else {
// TODO: Only throw ServerError for 5xx status codes.
throw new ServerError(networkResponse);
}
} else {
throw new NetworkError(networkResponse);
}
}
}
}
use of com.android.volley.NetworkResponse in project FastDev4Android by jiangqqlmj.
the class BasicNetwork method performRequest.
/**
* 执行网络请求
* @param request Request to process 需要处理的请求
* @return
* @throws VolleyError
*/
@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<String, String>();
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) {
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 (SocketTimeoutException e) {
attemptRetryOnException("socket", request, new TimeoutError());
} catch (ConnectTimeoutException e) {
attemptRetryOnException("connection", request, new TimeoutError());
} catch (MalformedURLException e) {
throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (IOException e) {
int statusCode = 0;
NetworkResponse networkResponse = null;
if (httpResponse != null) {
statusCode = httpResponse.getStatusLine().getStatusCode();
} else {
throw new NoConnectionError(e);
}
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
if (responseContents != null) {
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 {
// TODO: Only throw ServerError for 5xx status codes.
throw new ServerError(networkResponse);
}
} else {
throw new NetworkError(networkResponse);
}
}
}
}
Aggregations