Volley won't cache my requests
Asked Answered
H

0

0

I am trying to cache data for offline usage without success. Below is my work so far.

  //fetch news details
private void FetchHeadline(){

    showDialog();
    btnRefresh.setVisibility(View.GONE);

    // We first check for cached request
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(NEWS_URL);
if (entry != null) {
    Log.e("", "babaaaaaaaaaaaaaaaaaaaaaaaa");
    // fetch the data from cache
    try {
        String data = new String(entry.data, "UTF-8");
        try {
            parseJsonFeed(new JSONArray(data));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

} else {

    // making fresh volley request and getting json
    JsonArrayRequest jsonReq = new JsonArrayRequest(
            NEWS_URL + dbHelper.getAuth().getString(0), new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            Log.e(TAG, "Response: " + response.toString());
            if (response != null) {
                parseJsonFeed(response);
            }
            hideDialog();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            btnRefresh.setVisibility(View.VISIBLE);
            hideDialog();
            Log.e(TAG, "Response: " + error.getMessage());

            try {
                if (error.getMessage().equalsIgnoreCase("java.io.IOException: No authentication challenges found")){
                    moveToLoginActivity();}
            }catch(Exception e){}
        }
    });
    // Adding request to volley request queue
    AppController.getInstance().addToRequestQueue(jsonReq);
}

I added the following to my .htaccess with no success

    <IfModule headers_module>
    # 1 Month for most static assets
    <filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
    Header set Cache-Control "max-age=36000, public"
    </filesMatch>
</IfModule>
Hayleyhayloft answered 10/6, 2016 at 3:15 Comment(6)
@cricket_007 Didn't solve my problemHayleyhayloft
Did you read the answer? Does the server have the cache control header?Involution
The server returned this: Cache-Control →private, must-revalidate, max-age=36000, publicHayleyhayloft
Try debugging, set breakpoint at // Cache-Control takes precedence over an Expires header, even if both exist and Expires // is more restrictive. if (hasCacheControl) { inside parseCacheHeaders method of HttpHeaderParser.java fileYurev
Okay, so if you are making a volley request to any html page or rest api, then you don't have a cache header since you've only cached the file types listed in the htaccessInvolution
It is an API (Laravel) and request goes through the index.php. Is it possible to specify some routes (e.g myserver.com/api/v1/cars) instead of the whole index.php in the .htaccess file?Hayleyhayloft

© 2022 - 2024 — McMap. All rights reserved.