Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Cache cache = AppController.getInstance().getRequestQueue().getCache();
- Entry entry = cache.get(URL_FEED);
- if (entry != null) {
- // fetch the data from cache
- try {
- String data = new String(entry.data, "UTF-8");
- try {
- parseJsonFeed(new JSONObject(data));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- } else {
- // making fresh volley request and getting json
- JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
- URL_FEED, null, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- VolleyLog.d(TAG, "InitialResponse: " + response.toString());
- if (response != null) {
- parseJsonFeed(response);
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- VolleyLog.d(TAG, "Error: " + error.getMessage());
- }
- });
- // Adding request to volley request queue
- AppController.getInstance().addToRequestQueue(jsonReq);
- }
- private void parseJsonFeed(JSONObject response) {
- try {
- JSONArray feedArray = response.getJSONArray("feed");
- for (int i = 0; i < feedArray.length(); i++) {
- JSONObject feedObj = (JSONObject) feedArray.get(i);
- FeedItem item = new FeedItem();
- item.setId(feedObj.getInt("id"));
- item.setName(feedObj.getString("name"));
- // Image might be null sometimes
- String image = feedObj.isNull("image") ? null : feedObj
- .getString("image");
- item.setImge(image);
- item.setStatus(feedObj.getString("status"));
- item.setProfilePic(feedObj.getString("profilePic"));
- item.setTimeStamp(feedObj.getString("timeStamp"));
- // url might be null sometimes
- String feedUrl = feedObj.isNull("url") ? null : feedObj
- .getString("url");
- item.setUrl(feedUrl);
- feedItems.add(item);
- }
- // notify data changes to list adapater
- listAdapter.notifyDataSetChanged();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- public class EndlessListView extends ListView implements OnScrollListener {
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- }
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {}
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- if (getAdapter() == null)
- return ;
- if (getAdapter().getCount() == 0)
- return ;
- int l = visibleItemCount + firstVisibleItem;
- if (l >= totalItemCount && !isLoading) {
- // It is time to add new data. We call the listener
- this.addFooterView(footer);
- isLoading = true;
- listener.loadData();
- }
- }
- public static interface EndlessListener {
- public void loadData() ;
- }
Add Comment
Please, Sign In to add comment