收到304

时间:2015-11-09 09:57:56

标签: android caching android-volley android-networking

我正在尝试使用VolleyCache工作。即使设置了304,当我收到getCacheEntry().data null时也会"cache-control"。这就是我在做的事情:

  1. Volley获得实例化,如此

    // Instantiate the cache
    Cache cache = new DiskBasedCache(c.getCacheDir(), 10 * 1024 * 1024); // 10 MB cap
    
    // Set up the network to use HttpURLConnection as the HTTP client.
    Network network = new BasicNetwork(new HurlStack());
    
    // Instantiate the RequestQueue with the cache and network.
    mRequestQueue = new RequestQueue(cache, network);
    
    // Start the queue
    mRequestQueue.start();
    
  2. 发送GET请求后,我的回复200的{​​{1}}设置为"cache-control"。到目前为止还不错?

  3. 如果GET请求被提出两次或更多次,我会将"max-age=180, public"设置为最后一个时间戳,并请求请求标头。
  4. 我第二次请求特定的API端点时,服务器将使用"If-Modified-Since"进行响应。 304会返回getCacheEntry().data。如果我查看null cache entries中的Volleys,则无法找到符合我特定要求的条目。
  5. 我做错了什么?出于某种原因,我有一个请求,在触发一次时总是被缓存。它甚至可以返回大量数据。但所有其他请求都没有缓存。以下代码段解析响应并检查RequestQueue

    304

    我真的很感激对此发表评论。

1 个答案:

答案 0 :(得分:0)

恕我直言,您的缓存条目始终为空(不仅在获取304 resp代码时),原因如下:

c.getCacheDir()

请检查您的WRITE_EXTERNAL_STORAGE以查看是否要使用外部存储来存储缓存数据,然后您应在AndroidManifest.xml文件中设置<div id="tab3"> <h2>Chat Room</h2> <div id="chatboxlog"> <div id="chatlog"> Loading chat please wait... </div> </div> <div id="chatinput"> <form name="chatbox" class="userchat"> <input class="userchat" name="message" type="text" onkeydown="if (event.keyCode == 13) document.getElementById('chatbutton').click()"/><br> <input class="userchat" id="chatbutton" name="submitmsg" type="button" onclick="submitChat()" value="Send" /> </form> </div> </div> <script> function submitChat() { if(chatbox.message.value == '') { alert('Error: Missing Fields.'); return; } var message = chatbox.message.value; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState==4&&xmlhttp.status==100) { document.getElementById('chatlog').innerHTML = xmlhttp.responseText; } } xmlhttp.open('GET','chat.php?message='+message, true); xmlhttp.send(); chatbox.reset(); } $(document).ready(function(e) { $.ajaxSetup({cache:false}); setInterval(function() {$('#chatlog').load('logs.php');}, 200); }); </script> 权限。

希望这有帮助!

相关问题