android – Cache.Entry没有获取json数据

前端之家收集整理的这篇文章主要介绍了android – Cache.Entry没有获取json数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用volly来完美地检索数据及其工作,除了我的json数组没有存储在缓存中.

这是我的代码

  1. private void getCacheValue() {
  2. Cache cache = AppController.getInstance().getRequestQueue().getCache();
  3. Cache.Entry entry = cache.get(Endpoints.product_url);
  4.  
  5.  
  6. if(entry != null){
  7. Log.w("Logdata:",""+ entry.toString());
  8. try {
  9. String data = new String(entry.data,"UTF-8");
  10. JSONArray jsonArray = new JSONArray(data);
  11.  
  12. // handle data,like converting it to xml,json,bitmap etc.,Log.v("Hello",data);
  13. listProduct.clear();
  14. for (int i = 0; i < jsonArray.length(); i++) {
  15. try {
  16. JSONObject object = jsonArray.getJSONObject(i);
  17. ItemCategories image = new ItemCategories();
  18. image.setCategoryItem(object.getString(key_title));
  19. image.setUrlThumb(object.getString(key_image));
  20.  
  21. listProduct.add(image);
  22. } catch (JSONException e) {
  23. Log.e(TAG,"Json parsing error: " + e.getMessage());
  24. }
  25. }
  26.  
  27. adapterProductList.notifyDataSetChanged();
  28. progressBarMain.setVisibility(View.GONE);
  29. internetError.setVisibility(View.GONE);
  30. recycleProductList.setVisibility(View.VISIBLE);
  31.  
  32. } catch (UnsupportedEncodingException e) {
  33. e.printStackTrace();
  34. } catch (JSONException e) {
  35. e.printStackTrace();
  36. Log.v("JSON EXCEPTION","DECLINE");
  37. }
  38. } else {
  39. if(isNetworkAvailable()) {
  40. fetchImages();
  41. } else {
  42. progressBarMain.setVisibility(View.GONE);
  43. internetError.setVisibility(View.VISIBLE);
  44. }
  45. }
  46. }
  47.  
  48. private void fetchImages() {
  49. JsonObjectRequest jsObjRequest =
  50. new JsonObjectRequest(Request.Method.GET,Endpoints.product_url(String) null,new Response.Listener<JSONObject>() {
  51. @Override
  52. public void onResponse(JSONObject response) {
  53. listProduct.clear();
  54. try {
  55. JSONArray routes = response.getJSONArray(key_product);
  56. for (int i = 0; i < routes.length(); i++) {
  57. JSONObject object = routes.getJSONObject(i);
  58. ItemCategories categories = new ItemCategories();
  59. categories.setCategoryItem(object.getString(key_title));
  60. categories.setUrlThumb(object.getString(key_image));
  61.  
  62. listProduct.add(categories);
  63. }
  64. adapterProductList.notifyDataSetChanged();
  65. progressBarMain.setVisibility(View.GONE);
  66. internetError.setVisibility(View.GONE);
  67. recycleProductList.setVisibility(View.VISIBLE);
  68. } catch (JSONException e) {
  69. e.printStackTrace();
  70. Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
  71. }
  72. }
  73. },new Response.ErrorListener() {
  74. @Override
  75. public void onErrorResponse(VolleyError error) {
  76. VolleyLog.d(TAG,"Error: " + error.getMessage());
  77. }
  78. });
  79. // Adding request to request queue
  80. AppController.getInstance().addToRequestQueue(jsObjRequest);
  81. }

我已经在清单中声明了Appcontroller,但我不知道为什么缓存不起作用.

这是我的json_data

fetchImage()正在运行,因为recyclerview中有数据.但是,当我尝试脱机检索数据时,它会显示任何数据,因为我的缓存无法存储任何数据.

解决方法

默认情况下,如果响应标头允许,Volley仅缓存数据.
Volley根据以下响应标头缓存响应:
1.缓存控制
2.到期
3. maxAge

请参阅以下功能了解详情:

  1. public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
  2. long now = System.currentTimeMillis();
  3.  
  4. Map<String,String> headers = response.headers;
  5.  
  6. long serverDate = 0;
  7. long lastModified = 0;
  8. long serverExpires = 0;
  9. long softExpire = 0;
  10. long finalExpire = 0;
  11. long maxAge = 0;
  12. long staleWhileRevalidate = 0;
  13. boolean hasCacheControl = false;
  14. boolean mustRevalidate = false;
  15.  
  16. String serverEtag = null;
  17. String headerValue;
  18.  
  19. headerValue = headers.get("Date");
  20. if (headerValue != null) {
  21. serverDate = parseDateAsEpoch(headerValue);
  22. }
  23.  
  24. headerValue = headers.get("Cache-Control");
  25. if (headerValue != null) {
  26. hasCacheControl = true;
  27. String[] tokens = headerValue.split(",");
  28. for (int i = 0; i < tokens.length; i++) {
  29. String token = tokens[i].trim();
  30. if (token.equals("no-cache") || token.equals("no-store")) {
  31. return null;
  32. } else if (token.startsWith("max-age=")) {
  33. try {
  34. maxAge = Long.parseLong(token.substring(8));
  35. } catch (Exception e) {
  36. }
  37. } else if (token.startsWith("stale-while-revalidate=")) {
  38. try {
  39. staleWhileRevalidate = Long.parseLong(token.substring(23));
  40. } catch (Exception e) {
  41. }
  42. } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
  43. mustRevalidate = true;
  44. }
  45. }
  46. }
  47.  
  48. headerValue = headers.get("Expires");
  49. if (headerValue != null) {
  50. serverExpires = parseDateAsEpoch(headerValue);
  51. }
  52.  
  53. headerValue = headers.get("Last-Modified");
  54. if (headerValue != null) {
  55. lastModified = parseDateAsEpoch(headerValue);
  56. }
  57.  
  58. serverEtag = headers.get("ETag");
  59.  
  60. // Cache-Control takes precedence over an Expires header,even if both exist and Expires
  61. // is more restrictive.
  62. if (hasCacheControl) {
  63. softExpire = now + maxAge * 1000;
  64. finalExpire = mustRevalidate
  65. ? softExpire
  66. : softExpire + staleWhileRevalidate * 1000;
  67. } else if (serverDate > 0 && serverExpires >= serverDate) {
  68. // Default semantic for Expire header in HTTP specification is softExpire.
  69. softExpire = now + (serverExpires - serverDate);
  70. finalExpire = softExpire;
  71. }
  72.  
  73. Cache.Entry entry = new Cache.Entry();
  74. entry.data = response.data;
  75. entry.etag = serverEtag;
  76. entry.softTtl = softExpire;
  77. entry.ttl = finalExpire;
  78. entry.serverDate = serverDate;
  79. entry.lastModified = lastModified;
  80. entry.responseHeaders = headers;
  81.  
  82. return entry;
  83. }

您可以通过覆盖Request对象来更改默认缓存策略.
您可以覆盖JsonObjectRequest,如:

  1. public class CustomJsonObjectRequest extends JsonObjectRequest {
  2. public CustomJsonObjectRequest(int method,String url,JSONObject jsonRequest,Response.Listener<JSONObject> listener,Response.ErrorListener errorListener) {
  3. super(method,url,jsonRequest,listener,errorListener);
  4. }
  5.  
  6. public CustomJsonObjectRequest(String url,Response.ErrorListener errorListener) {
  7. super(url,errorListener);
  8. }
  9.  
  10. @Override
  11. protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
  12. try {
  13. String jsonString = new String(response.data,HttpHeaderParser.parseCharset(response.headers,PROTOCOL_CHARSET));
  14. return Response.success(new JSONObject(jsonString),parseIgnoreCacheHeaders(response));
  15. } catch (UnsupportedEncodingException e) {
  16. return Response.error(new ParseError(e));
  17. } catch (JSONException je) {
  18. return Response.error(new ParseError(je));
  19. }
  20. }
  21.  
  22. public static Cache.Entry parseIgnoreCacheHeaders(NetworkResponse response) {
  23. long now = System.currentTimeMillis();
  24. Map<String,String> headers = response.headers;
  25. long serverDate = 0;
  26. String serverEtag = null;
  27. String headerValue;
  28.  
  29. headerValue = headers.get("Date");
  30. if (headerValue != null) {
  31. serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
  32. }
  33.  
  34. serverEtag = headers.get("ETag");
  35.  
  36. final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit,but also refreshed on background
  37. final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
  38. final long softExpire = now + cacheHitButRefreshed;
  39. final long ttl = now + cacheExpired;
  40.  
  41. Cache.Entry entry = new Cache.Entry();
  42. entry.data = response.data;
  43. entry.etag = serverEtag;
  44. entry.softTtl = softExpire;
  45. entry.ttl = ttl;
  46. entry.serverDate = serverDate;
  47. entry.responseHeaders = headers;
  48.  
  49. return entry;
  50. }
  51. }

将fetchImage函数更新为:

  1. private void fetchImages() {
  2.  
  3. CustomJsonObjectRequest jsObjRequest = new CustomJsonObjectRequest()
  4. (Request.Method.GET,Endpoints.product_url,(String) null,new Response.Listener<JSONObject>() {
  5.  
  6. @Override
  7. public void onResponse(JSONObject response) {
  8. listProduct.clear();
  9. try {
  10. JSONArray routes = response.getJSONArray(key_product);
  11. for (int i = 0; i < routes.length(); i++) {
  12. JSONObject object = routes.getJSONObject(i);
  13. ItemCategories categories = new ItemCategories();
  14.  
  15. categories.setCategoryItem(object.getString(key_title));
  16. categories.setUrlThumb(object.getString(key_image));
  17.  
  18. listProduct.add(categories);
  19. }
  20. adapterProductList.notifyDataSetChanged();
  21. progressBarMain.setVisibility(View.GONE);
  22. internetError.setVisibility(View.GONE);
  23. recycleProductList.setVisibility(View.VISIBLE);
  24. } catch (JSONException e) {
  25. e.printStackTrace();
  26. Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG).show();
  27. }
  28. }
  29. },new Response.ErrorListener() {
  30. @Override
  31. public void onErrorResponse(VolleyError error) {
  32. VolleyLog.d(TAG,"Error: " + error.getMessage());
  33.  
  34. }
  35. });
  36. // Adding request to request queue
  37. AppController.getInstance().addToRequestQueue(jsObjRequest);
  38. }

作为参考,请检查Android Volley + JSONObjectRequest Caching

猜你在找的Android相关文章