Search in sources :

Example 36 with NetworkResponse

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;
    }
}
Also used : NetworkResponse(com.android.volley.NetworkResponse) Cache(com.android.volley.Cache)

Example 37 with NetworkResponse

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());
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ServerError(com.android.volley.ServerError) HttpResponse(org.apache.http.HttpResponse) AuthFailureError(com.android.volley.AuthFailureError) NetworkError(com.android.volley.NetworkError) NoConnectionError(com.android.volley.NoConnectionError) IOException(java.io.IOException) TimeoutError(com.android.volley.TimeoutError) StatusLine(org.apache.http.StatusLine) SocketTimeoutException(java.net.SocketTimeoutException) NetworkResponse(com.android.volley.NetworkResponse) Cache(com.android.volley.Cache) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 38 with NetworkResponse

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;
}
Also used : VolleyError(com.android.volley.VolleyError) NetworkResponse(com.android.volley.NetworkResponse)

Example 39 with NetworkResponse

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);
}
Also used : NetworkResponse(com.android.volley.NetworkResponse)

Example 40 with NetworkResponse

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);
}
Also used : NetworkResponse(com.android.volley.NetworkResponse) Before(org.junit.Before)

Aggregations

NetworkResponse (com.android.volley.NetworkResponse)46 Test (org.junit.Test)20 HashMap (java.util.HashMap)17 Cache (com.android.volley.Cache)9 JSONObject (org.json.JSONObject)9 AuthFailureError (com.android.volley.AuthFailureError)8 NetworkError (com.android.volley.NetworkError)8 NoConnectionError (com.android.volley.NoConnectionError)8 ServerError (com.android.volley.ServerError)8 TimeoutError (com.android.volley.TimeoutError)8 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)8 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 SocketTimeoutException (java.net.SocketTimeoutException)8 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)8 JSONArray (org.json.JSONArray)8 JsonArrayRequest (com.android.volley.toolbox.JsonArrayRequest)7 HttpResponse (org.apache.http.HttpResponse)7 StatusLine (org.apache.http.StatusLine)7 String (java.lang.String)6