use of com.android.volley.NetworkResponse in project android-volley by mcxiaoke.
the class JsonRequestCharsetTest method specifiedCharsetJsonObject.
@Test
public void specifiedCharsetJsonObject() throws Exception {
byte[] data = jsonObjectString().getBytes(Charset.forName("ISO-8859-1"));
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=iso-8859-1");
NetworkResponse network = new NetworkResponse(data, headers);
JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
assertNotNull(objectResponse);
assertTrue(objectResponse.isSuccess());
//don't check the text in Czech, ISO-8859-1 doesn't support some Czech characters
assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
}
use of com.android.volley.NetworkResponse in project iosched by google.
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 = new HashMap<String, String>();
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) {
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, request.getCacheEntry() == null ? null : request.getCacheEntry().data, responseHeaders, true);
}
// 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);
} 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);
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);
}
}
}
}
use of com.android.volley.NetworkResponse in project iosched by google.
the class HttpHeaderParserTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
headers = new HashMap<String, String>();
response = new NetworkResponse(0, null, headers, false);
}
use of com.android.volley.NetworkResponse in project WordPress-Android by wordpress-mobile.
the class StatsUtils method logVolleyErrorDetails.
public static synchronized void logVolleyErrorDetails(final VolleyError volleyError) {
if (volleyError == null) {
AppLog.e(T.STATS, "Tried to log a VolleyError, but the error obj was null!");
return;
}
if (volleyError.networkResponse != null) {
NetworkResponse networkResponse = volleyError.networkResponse;
AppLog.e(T.STATS, "Network status code: " + networkResponse.statusCode);
if (networkResponse.data != null) {
AppLog.e(T.STATS, "Network data: " + new String(networkResponse.data));
}
}
AppLog.e(T.STATS, "Volley Error Message: " + volleyError.getMessage(), volleyError);
}
use of com.android.volley.NetworkResponse in project android_frameworks_base by AOSPA.
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;
}
}
Aggregations