How do I cache from JSON data using Volley library? - android

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.

Related

Not able to fetch JSON data [using volley Library] in Android

The jason array request code goes like this:
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET,url, (JSONArray) null , new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
Log.d("Response:", String.valueOf(response.getJSONObject(0)));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "Response not recieved", Toast.LENGTH_SHORT).show();
}
});
And I've used a singleton instance of a request queue, to fetch the data,
AppController.getInstance(context.getApplicationContext()).addToRequestQueue(arrayRequest);
I'm using an api service that supplies a bunch of questions (The api generates a url, which returns a collection of JSON Objects, An Array (Just try and Open the url link, the json array will be seen)
But when I run the app, the request is not even getting a response,the progam flow is going into the error listner block.
Can someone explain this behaviour? Is it because the JSON array supplied by the url, has nested arrays? Why?
I tried reading and anlyzing other questions here, which might have the same issue, But i found nothing.
You want to just change JsonArrayRequest to JsonObjectRequest:
Please copy and paste below code:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String category = jsonObject.getString("category");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "Error: " + error.getMessage());
// hide the progress dialog
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq, "TAG");
Follow this link for learn other request: Androidhive

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.

volley cache is empty

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?

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.

Android Volley Synchronize Server SQLite

I am new to android development, just started with sqlite.
Working on location app that should retrieve locations from mongo server, and populate local SQLite db.
Listview, Search queries etc.. should use local db data.
I am using volley library for my networking.
Questions:
1- After getting volley response, how do i populate sqlite?
2- How do I send updated location data from server to device?
(I dont need to update server with device info.)
3- How do i ensure sqlite only gets updated, not overwriten from server everytime?
I have seen examples using asynctask, syncadapter etc... But at this
point in project i cannot change my code from volley.
My volley code below (currently parses json to object):
private void getResource() {
RequestQueue rq = Volley.newRequestQueue(this);
JsonArrayRequest jObjR = new JsonArrayRequest(url_all_points, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
parseJSON(response);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
}
);
rq.add(jObjR);
}
private void parseJSON(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObj = response.getJSONObject(i);
JSONObject coord = jsonObj.getJSONObject("coord");
MyLocation wj = new MyLocation();
wj.setTitle(jsonObj.optString("name"));
wj.setLat(coord.optDouble("latitude"));
wj.setLng(coord.optDouble("longitude"));
mLocations.add(wj);
LatLng location = new LatLng(wj.getmLat(), wj.getmLng());
mMap.addMarker(new MarkerOptions().position(location)
.title(wj.getTitle()));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
}
}

Categories

Resources