Search in sources :

Example 1 with Cache

use of com.android.volley.Cache in project FastDev4Android by jiangqqlmj.

the class HttpHeaderParserTest method parseCaseInsensitive.

@Test
public void parseCaseInsensitive() {
    long now = System.currentTimeMillis();
    Header[] headersArray = new Header[5];
    headersArray[0] = new BasicHeader("eTAG", "Yow!");
    headersArray[1] = new BasicHeader("DATE", rfc1123Date(now));
    headersArray[2] = new BasicHeader("expires", rfc1123Date(now + ONE_HOUR_MILLIS));
    headersArray[3] = new BasicHeader("cache-control", "public, max-age=86400");
    headersArray[4] = new BasicHeader("content-type", "text/plain");
    Map<String, String> headers = BasicNetwork.convertHeaders(headersArray);
    NetworkResponse response = new NetworkResponse(0, null, headers, false);
    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
    assertNotNull(entry);
    assertEquals("Yow!", entry.etag);
    assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
    assertEquals(entry.softTtl, entry.ttl);
    assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) NetworkResponse(com.android.volley.NetworkResponse) BasicHeader(org.apache.http.message.BasicHeader) Cache(com.android.volley.Cache) Test(org.junit.Test)

Example 2 with Cache

use of com.android.volley.Cache in project FastDev4Android by jiangqqlmj.

the class CacheTestUtils method makeRandomCacheEntry.

/**
     * Makes a random cache entry.
     * @param data Data to use, or null to use random data
     * @param isExpired Whether the TTLs should be set such that this entry is expired
     * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh
     */
public static Cache.Entry makeRandomCacheEntry(byte[] data, boolean isExpired, boolean needsRefresh) {
    Random random = new Random();
    Cache.Entry entry = new Cache.Entry();
    if (data != null) {
        entry.data = data;
    } else {
        entry.data = new byte[random.nextInt(1024)];
    }
    entry.etag = String.valueOf(random.nextLong());
    entry.lastModified = random.nextLong();
    entry.ttl = isExpired ? 0 : Long.MAX_VALUE;
    entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE;
    return entry;
}
Also used : Random(java.util.Random) Cache(com.android.volley.Cache)

Example 3 with Cache

use of com.android.volley.Cache in project android-volley by mcxiaoke.

the class CacheTestUtils method makeRandomCacheEntry.

/**
     * Makes a random cache entry.
     * @param data Data to use, or null to use random data
     * @param isExpired Whether the TTLs should be set such that this entry is expired
     * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh
     */
public static Cache.Entry makeRandomCacheEntry(byte[] data, boolean isExpired, boolean needsRefresh) {
    Random random = new Random();
    Cache.Entry entry = new Cache.Entry();
    if (data != null) {
        entry.data = data;
    } else {
        entry.data = new byte[random.nextInt(1024)];
    }
    entry.etag = String.valueOf(random.nextLong());
    entry.lastModified = random.nextLong();
    entry.ttl = isExpired ? 0 : Long.MAX_VALUE;
    entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE;
    return entry;
}
Also used : Random(java.util.Random) Cache(com.android.volley.Cache)

Example 4 with Cache

use of com.android.volley.Cache in project iosched by google.

the class CacheTestUtils method makeRandomCacheEntry.

/**
     * Makes a random cache entry.
     * @param data Data to use, or null to use random data
     * @param isExpired Whether the TTLs should be set such that this entry is expired
     * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh
     */
public static Cache.Entry makeRandomCacheEntry(byte[] data, boolean isExpired, boolean needsRefresh) {
    Random random = new Random();
    Cache.Entry entry = new Cache.Entry();
    if (data != null) {
        entry.data = data;
    } else {
        entry.data = new byte[random.nextInt(1024)];
    }
    entry.etag = String.valueOf(random.nextLong());
    entry.serverDate = random.nextLong();
    entry.ttl = isExpired ? 0 : Long.MAX_VALUE;
    entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE;
    return entry;
}
Also used : Random(java.util.Random) Cache(com.android.volley.Cache)

Example 5 with Cache

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

Aggregations

Cache (com.android.volley.Cache)15 NetworkResponse (com.android.volley.NetworkResponse)9 Random (java.util.Random)4 Header (org.apache.http.Header)3 BasicHeader (org.apache.http.message.BasicHeader)3 Test (org.junit.Test)3 AuthFailureError (com.android.volley.AuthFailureError)1 Network (com.android.volley.Network)1 NetworkError (com.android.volley.NetworkError)1 NoConnectionError (com.android.volley.NoConnectionError)1 RequestQueue (com.android.volley.RequestQueue)1 ServerError (com.android.volley.ServerError)1 TimeoutError (com.android.volley.TimeoutError)1 BasicNetwork (com.android.volley.toolbox.BasicNetwork)1 DiskBasedCache (com.android.volley.toolbox.DiskBasedCache)1 HurlStack (com.android.volley.toolbox.HurlStack)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1