volley cache is empty - android

Im working on with volley, i have issue in retrieving the cache. Every time I'll to access the cache for display It is empty. But when i check it on the sdcard I can see a files under cache folder in my application.
// We first check for cached request
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(URL_DASHBOARD);
AppController.getInstance().getRequestQueue().getCache().invalidate(URL_DASHBOARD, true);
if (entry != null) {
// fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
try {
JSONObject temp = new JSONObject(data);
Log.e(TAG,"loadVolleyConnection: fetch data from cache: "+temp.toString());
setAdapter_dashboard(temp.getJSONArray(JSON_DASHBOARD.ARRAY_DASHBOARD.getVal().toString()));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
Log.e(TAG,"loadVolleyConnection: query to api “);
executeVolleyRequest(URL_DASHBOARD);
}
executeVolleyRequest
/**Execute Volley*/
private void executeVolleyRequest(String path){
//Making fresh volley request and getting json array
String tag_string_req = "string_req";
StringRequest stringRequest = new StringRequest(Request.Method.POST,path,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e(TAG,"loadVolleyConnection: fetch data from api: "+response);
try {
JSONObject temp_obj = new JSONObject(response);
JSONArray temp_array = temp_obj.getJSONArray(JSON_DASHBOARD.ARRAY_DASHBOARD.getVal().toString());
setAdapter_dashboard(temp_array);
if(isRefreshed){
// Call onRefreshComplete when the list has been refreshed.
(myListView).onRefreshComplete();
isRefreshed = false;
setUpdate(temp_array);
Log.e(TAG,"loadVolleyConnection: isRefreshed..");
}else{
setAdapter_dashboard(temp_array);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.e(TAG, "Error: " + volleyError.getClass().getSimpleName());
if (volleyError.networkResponse == null) {
if (volleyError.getClass().equals(TimeoutError.class)) {
// Show timeout error message
Alertmessage("Login Error","Oops.Timeout error!Please check your connection.");
}
}else{
Alertmessage("Login Error","Check your Connection./n");
}
//pDialog.dismiss();
}
}){
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000,2,1));
stringRequest.setShouldCache(true);
// Adding request to request queue
AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
}
I can't figure out what i am missing.
thanks

After, testing for a couple of times, i found out that when you use volley and when you want to have a cache, you need to use a GET request not POST.
But my question is, is there a way that we can still use cache when using post?

Related

How to retrieve text from JSON

I'm trying to retrieve a token from a JSON response. I've successfully managed to get a response, but I don't know how to retrieve the token.
Here's my code
private void TmdbAuth() {
final String TokenUrl = "https://api.themoviedb.org/3/authentication/token/new?api_key=<<api key goes here>>";
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
TokenUrl,
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
Log.i("jjj", response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
);
requestQueue.add(jsonObjectRequest);
}
and the response of this json
{"success":true,"expires_at":"2019-04-01 10:49:44 UTC","request_token":"request token goes here"}
How do I retrieve only the request_token
Try this #tony
JsonObject object = new JsonObject(response); //obtained response from the server.
String request_token = object.getString("request_token");
Log.e("request_token",request_token); //Just for checking in the logcat.
You can access token String this way
String token = response.optString("request_token");
You can try like this
try{
Log.i("jjj", response.toString());
String requestToken = response.getString("request_token");
}
catch (Exception e) {
e.printStackTrace();
}

Volley won't cache my requests

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>

How do I cache from JSON data using Volley library?

This is my JSON code. I want to cache JSON data from URL for the first time and second time it will be work without internet.How and where have to use the volley cache tool, and how to access the saved file??
StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.URL_CONTACTS, new Response.Listener<String>() {
#Override
public void onResponse(final String response) {
new Thread(new Runnable() {
#Override
public void run() {
try{
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.has(Constants.TAG_EXAM)) {
JSONArray examArray = jsonObject.getJSONArray(Constants.TAG_EXAM);
for(int i = 0; i < examArray.length(); i++) {
// get all array from JSON object
JSONObject seat = examArray.getJSONObject(i);
}
}
handler.sendEmptyMessage(1);
} catch (JSONException e) {
handler.sendEmptyMessage(2);
e.printStackTrace();
} catch (NullPointerException e) {
handler.sendEmptyMessage(3);
e.printStackTrace();
}
}
}).start();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}``
})`
Volley doesn't provide such cache tools to cache json,you can create your own cache tool using map or LruCache to make it.because your json will be little and use little memory,you can use map to save it,it will be the easiest way to make it in my opinion.
You can cache the response of volley since it's a string using SharedPreferences.

Cannot assign value to final Volley variable response

I am familiar with Volley and creating a Singleton class, and adding requests to the queue. However, I wish to increase the modularity of volley and simply call all requests through method call to another class instead. I have setup the present action as basis for the common GET request:
public Object getRequest(String params) {
final JSONObject getRequestReturn = new JSONObject();
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET,
VolleySingleton.prefixURL, ((String) null),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// Parse the JSON:
try {
getRequestReturn = response;
Log.v("GET Request value", response.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("GET Request Error", error.toString());
}
});
mRequestQueue.add(getRequest);
return getRequestReturn;
}
However, I have a the perplexing catch 22 error on the assignment of response at:
getRequestReturn = response;
The error notes that getRequestReturn must be declared final to allow for use within the inner class, but upon assigning final, another error appears noting that you cannot assign a value to a final variable.
How can this method be handled?
Declare JSONObject as global and initialize in same place like this.
JSONObject getRequestReturn ;
getRequestReturn = new JSONObject();
public Object getRequest(String params) {
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET,
VolleySingleton.prefixURL, ((String) null),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// Parse the JSON:
try {
Log.v("GET Request value", response.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("GET Request Error", error.toString());
}
});
mRequestQueue.add(getRequest);
return new JSONObject();
}
U can't get response when u return!Volley request is async!
I use EventBus and I do it like this :
When I need to get data from web , i add a request like this.
Then when i get response from web ,I use EventBus to post a event.
I get the event and update my page.
Or U can try the RxAndroid.

Android volley saving json data to string for sharing (onPostExecute method for volley?)

I am using volley library to get data from an api in JSON format, and it works like a charm. I ask for JSON file, i get a response, i parse that respons and populte the views in activity.
Now, i have a button to share the data using Intetnt.ACTION_SEND and putExtra(Intent.EXTRA_TEXT) that takes string as the 2nd parameter.
The problem is, i make a string variable and append data in that string variable inside volley on response method. but as volley makes a new thread for getting data from api the string is not updated and Intetnt.EXTRA_TEXT sends an empty string.
I want to ask, if there is anything similer to onPostExecute method for volley? where i can set the data to some variables after the thread is done processing. or any alternate methods to do the same.
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, bookSearchString, null,
new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
try {
JSONArray bookArray = response
.getJSONArray("items");
JSONObject bookObject = bookArray.getJSONObject(0);
try {
bookSelfLink = bookObject.getString("selfLink");
JsonObjectRequest newJsObjRequest = new JsonObjectRequest(
Request.Method.GET, bookSelfLink, null,
new Response.Listener<JSONObject>() {
public void onResponse(
JSONObject response) {
try {
JSONObject newBookObject = response
.getJSONObject("volumeInfo");
try {
dBookPages.setText("Pages - "
+ newBookObject
.getString("pageCount"));
eMailText = eMailText + newBookObject
.getString("pageCount"));
} catch (JSONException jse) {
dBookPages
.setText("Pages not found");
jse.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(
VolleyError error) {
// TODO Auto-generated method
// stub
}
});
AppController.getInstance().addToRequestQueue(
newJsObjRequest, tag_json_obj);
} catch (JSONException jse) {
jse.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
AppController.getInstance().addToRequestQueue(jsObjRequest,
tag_json_obj);
from the Volley v1.0.0 Documentation:
public boolean hasHadResponseDelivered()
Returns true if this request has had a response delivered for it.
I hope that this answer would help someone with the same question.
There were 2 work around for the record.
1) make network call on button click -> wait for response -> fire intent inside onResponse with bundled data.
2) pass url as string in bundle -> call intent for new activity -> make network request pars data.

Categories

Resources