com.android.volley.Cache.Entry Java Examples

The following examples show how to use com.android.volley.Cache.Entry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BasicNetwork.java    From volley with Apache License 2.0 6 votes vote down vote up
private Map<String, String> getCacheHeaders(Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return Collections.emptyMap();
    }

    Map<String, String> headers = new HashMap<>();

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        headers.put(
                "If-Modified-Since", HttpHeaderParser.formatEpochAsRfc1123(entry.lastModified));
    }

    return headers;
}
 
Example #2
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void headersAndPostParams() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    InputStream responseStream =
            new ByteArrayInputStream("foobar".getBytes(StandardCharsets.UTF_8));
    HttpResponse fakeResponse =
            new HttpResponse(200, Collections.<Header>emptyList(), 6, responseStream);
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    Entry entry = new Entry();
    entry.etag = "foobar";
    entry.lastModified = 1503102002000L;
    request.setCacheEntry(entry);
    httpNetwork.performRequest(request);
    assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
    assertEquals("foobar", mockHttpStack.getLastHeaders().get("If-None-Match"));
    assertEquals(
            "Sat, 19 Aug 2017 00:20:02 GMT",
            mockHttpStack.getLastHeaders().get("If-Modified-Since"));
    assertEquals(
            "requestpost=foo&",
            new String(mockHttpStack.getLastPostBody(), StandardCharsets.UTF_8));
}
 
Example #3
Source File: BasicNetwork.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        // 设置 If-None-Match
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        // 设置 If-Modified-Since
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #4
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 5 votes vote down vote up
@Test
public void notModified() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    List<Header> headers = new ArrayList<>();
    headers.add(new Header("ServerKeyA", "ServerValueA"));
    headers.add(new Header("ServerKeyB", "ServerValueB"));
    headers.add(new Header("SharedKey", "ServerValueShared"));
    headers.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    headers.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    HttpResponse fakeResponse = new HttpResponse(HttpURLConnection.HTTP_NOT_MODIFIED, headers);
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    Entry entry = new Entry();
    entry.allResponseHeaders = new ArrayList<>();
    entry.allResponseHeaders.add(new Header("CachedKeyA", "CachedValueA"));
    entry.allResponseHeaders.add(new Header("CachedKeyB", "CachedValueB"));
    entry.allResponseHeaders.add(new Header("SharedKey", "CachedValueShared"));
    entry.allResponseHeaders.add(new Header("SHAREDCASEINSENSITIVEKEY", "CachedValueShared1"));
    entry.allResponseHeaders.add(new Header("shAREDcaSEinSENSITIVEkeY", "CachedValueShared2"));
    request.setCacheEntry(entry);
    NetworkResponse response = httpNetwork.performRequest(request);
    List<Header> expectedHeaders = new ArrayList<>();
    // Should have all server headers + cache headers that didn't show up in server response.
    expectedHeaders.add(new Header("ServerKeyA", "ServerValueA"));
    expectedHeaders.add(new Header("ServerKeyB", "ServerValueB"));
    expectedHeaders.add(new Header("SharedKey", "ServerValueShared"));
    expectedHeaders.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    expectedHeaders.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    expectedHeaders.add(new Header("CachedKeyA", "CachedValueA"));
    expectedHeaders.add(new Header("CachedKeyB", "CachedValueB"));
    assertThat(expectedHeaders, containsInAnyOrder(response.allHeaders.toArray(new Header[0])));
}
 
Example #5
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 5 votes vote down vote up
@Test
public void notModified_legacyCache() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    List<Header> headers = new ArrayList<>();
    headers.add(new Header("ServerKeyA", "ServerValueA"));
    headers.add(new Header("ServerKeyB", "ServerValueB"));
    headers.add(new Header("SharedKey", "ServerValueShared"));
    headers.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    headers.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    HttpResponse fakeResponse = new HttpResponse(HttpURLConnection.HTTP_NOT_MODIFIED, headers);
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    Entry entry = new Entry();
    entry.responseHeaders = new HashMap<>();
    entry.responseHeaders.put("CachedKeyA", "CachedValueA");
    entry.responseHeaders.put("CachedKeyB", "CachedValueB");
    entry.responseHeaders.put("SharedKey", "CachedValueShared");
    entry.responseHeaders.put("SHAREDCASEINSENSITIVEKEY", "CachedValueShared1");
    entry.responseHeaders.put("shAREDcaSEinSENSITIVEkeY", "CachedValueShared2");
    request.setCacheEntry(entry);
    NetworkResponse response = httpNetwork.performRequest(request);
    List<Header> expectedHeaders = new ArrayList<>();
    // Should have all server headers + cache headers that didn't show up in server response.
    expectedHeaders.add(new Header("ServerKeyA", "ServerValueA"));
    expectedHeaders.add(new Header("ServerKeyB", "ServerValueB"));
    expectedHeaders.add(new Header("SharedKey", "ServerValueShared"));
    expectedHeaders.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    expectedHeaders.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    expectedHeaders.add(new Header("CachedKeyA", "CachedValueA"));
    expectedHeaders.add(new Header("CachedKeyB", "CachedValueB"));
    assertThat(expectedHeaders, containsInAnyOrder(response.allHeaders.toArray(new Header[0])));
}
 
Example #6
Source File: BasicNetwork.java    From device-database with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #7
Source File: BasicNetwork.java    From SimplifyReader with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #8
Source File: BaseFragment.java    From school_shop with MIT License 5 votes vote down vote up
/**
 * 
 * @author Owater
 * @createtime 2015-3-27 下午11:56:42
 * @Decription 获取缓存
 *
 * @param url
 * @return
 */
protected String getCache(String url){
	Entry entry = requestQueue.getCache().get(url);
	String jsonData = null;
	if(entry!=null){
		try {
			jsonData = new String(entry.data,Constants.ENCODING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	return jsonData;
}
 
Example #9
Source File: BasicNetwork.java    From TitanjumNote with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #10
Source File: BasicNetwork.java    From product-emm with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #11
Source File: BasicNetwork.java    From product-emm with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #12
Source File: BasicNetwork.java    From volley_demo with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #13
Source File: BasicNetwork.java    From volley with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.serverDate > 0) {
        Date refTime = new Date(entry.serverDate);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #14
Source File: BasicNetwork.java    From jus with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #15
Source File: BasicNetwork.java    From okulus with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.serverDate > 0) {
        Date refTime = new Date(entry.serverDate);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
Example #16
Source File: BasicNetwork.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}