I'm using volly to retrieve data and its work perfectly, except that my json array is not storing in the cache.
Here is my code:
private void getCacheValue() {
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(Endpoints.product_url);
if(entry != null){
Log.w("Logdata:", ""+ entry.toString());
try {
String data = new String(entry.data, "UTF-8");
JSONArray jsonArray = new JSONArray(data);
// handle data, like converting it to xml, json, bitmap etc.,
Log.v("Hello", data);
listProduct.clear();
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject object = jsonArray.getJSONObject(i);
ItemCategories image = new ItemCategories();
image.setCategoryItem(object.getString(key_title));
image.setUrlThumb(object.getString(key_image));
listProduct.add(image);
} catch (JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
}
adapterProductList.notifyDataSetChanged();
progressBarMain.setVisibility(View.GONE);
internetError.setVisibility(View.GONE);
recycleProductList.setVisibility(View.VISIBLE);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
Log.v("JSON EXCEPTION", "DECLINE");
}
} else {
if(isNetworkAvailable()) {
fetchImages();
} else {
progressBarMain.setVisibility(View.GONE);
internetError.setVisibility(View.VISIBLE);
}
}
}
private void fetchImages() {
JsonObjectRequest jsObjRequest =
new JsonObjectRequest(Request.Method.GET, Endpoints.product_url(String) null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
listProduct.clear();
try {
JSONArray routes = response.getJSONArray(key_product);
for (int i = 0; i < routes.length(); i++) {
JSONObject object = routes.getJSONObject(i);
ItemCategories categories = new ItemCategories();
categories.setCategoryItem(object.getString(key_title));
categories.setUrlThumb(object.getString(key_image));
listProduct.add(categories);
}
adapterProductList.notifyDataSetChanged();
progressBarMain.setVisibility(View.GONE);
internetError.setVisibility(View.GONE);
recycleProductList.setVisibility(View.VISIBLE);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsObjRequest);
}
I have already declared Appcontroller
in the manifest, but I don't know why caching is not working.
Here is my json_data
fetchImage()
is working because there is data in recyclerview
. However, when I try to retrieve the data offline it does show any because my cache can't store any data.