Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. import adapter.FeedListAdapter;
  2. import app.AppController;
  3. import data.FeedItem;
  4. import java.io.UnsupportedEncodingException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10. import android.annotation.SuppressLint;
  11. import android.app.Activity;
  12. import android.os.Bundle;
  13. import android.support.v4.widget.SwipeRefreshLayout;
  14. import android.view.Window;
  15. import android.view.WindowManager;
  16. import android.widget.ListView;
  17. import com.android.volley.Cache;
  18. import com.android.volley.Cache.Entry;
  19. import com.android.volley.Request.Method;
  20. import com.android.volley.Response;
  21. import com.android.volley.VolleyError;
  22. import com.android.volley.VolleyLog;
  23. import com.android.volley.toolbox.JsonObjectRequest;
  24.  
  25. public class MainActivity extends Activity {
  26. private static final String TAG = MainActivity.class.getSimpleName();
  27. private ListView listView;
  28. private FeedListAdapter listAdapter;
  29. private List<FeedItem> feedItems;
  30. private String URL_FEED = "http://myozawoo.esy.es/data.php";
  31. private String URL_FEED2 = "http://api.androidhive.info/feed/feed.json";
  32.  
  33. private SwipeRefreshLayout swipeContainer;
  34.  
  35. // String page = getIntent().getExtras().getString("page");
  36. @SuppressLint("NewApi")
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. requestWindowFeature(Window.FEATURE_NO_TITLE);
  41. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0);
  42. setContentView(R.layout.activity_main);
  43.  
  44. // String page = getIntent().getExtras().getString("page");
  45.  
  46. swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
  47. swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  48. @Override
  49. public void onRefresh() {
  50.  
  51.  
  52. listAdapter.notifyDataSetChanged();
  53. }
  54. });
  55.  
  56.  
  57. listView = (ListView) findViewById(R.id.list);
  58.  
  59. feedItems = new ArrayList<FeedItem>();
  60.  
  61. listAdapter = new FeedListAdapter(this, feedItems);
  62. listView.setAdapter(listAdapter);
  63.  
  64. // These two lines not needed,
  65. // just to get the look of facebook (changing background color & hiding the icon)
  66. // getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3b5998")));
  67. // getActionBar().setIcon(
  68. // new ColorDrawable(getResources().getColor(android.R.color.transparent)));
  69.  
  70. // We first check for cached request
  71. // Cache cache = AppController.getInstance().getRequestQueue().getCache();
  72.  
  73. // Page One
  74. String page = getIntent().getExtras().getString("page");
  75. if(page.equals("1")) {
  76. Cache cache = AppController.getInstance().getRequestQueue().getCache();
  77. Entry entry = cache.get(URL_FEED);
  78. if (entry != null) {
  79. // fetch the data from cache
  80. try {
  81. String data = new String(entry.data, "UTF-8");
  82. try {
  83. parseJsonFeed(new JSONObject(data));
  84. } catch (JSONException e) {
  85. e.printStackTrace();
  86. }
  87. } catch (UnsupportedEncodingException e) {
  88. e.printStackTrace();
  89. }
  90.  
  91. } else {
  92. // making fresh volley request and getting json
  93. JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
  94. URL_FEED, null, new Response.Listener<JSONObject>() {
  95.  
  96. @Override
  97. public void onResponse(JSONObject response) {
  98. VolleyLog.d(TAG, "Response: " + response.toString());
  99. if (response != null) {
  100. parseJsonFeed(response);
  101. }
  102. }
  103. }, new Response.ErrorListener() {
  104.  
  105. @Override
  106. public void onErrorResponse(VolleyError error) {
  107. VolleyLog.d(TAG, "Error: " + error.getMessage());
  108. }
  109. });
  110.  
  111. // Adding request to volley request queue
  112. AppController.getInstance().addToRequestQueue(jsonReq);
  113. }
  114. }
  115.  
  116. //Page Two
  117.  
  118. else if (page.equals("2")) {
  119. Cache cache = AppController.getInstance().getRequestQueue().getCache();
  120. Entry entry = cache.get(URL_FEED2);
  121. if (entry != null) {
  122. // fetch the data from cache
  123. try {
  124. String data = new String(entry.data, "UTF-8");
  125. try {
  126. parseJsonFeed(new JSONObject(data));
  127. } catch (JSONException e) {
  128. e.printStackTrace();
  129. }
  130. } catch (UnsupportedEncodingException e) {
  131. e.printStackTrace();
  132. }
  133.  
  134. } else {
  135. // making fresh volley request and getting json
  136. JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
  137. URL_FEED2, null, new Response.Listener<JSONObject>() {
  138.  
  139. @Override
  140. public void onResponse(JSONObject response) {
  141. VolleyLog.d(TAG, "Response: " + response.toString());
  142. if (response != null) {
  143. parseJsonFeed(response);
  144. }
  145. }
  146. }, new Response.ErrorListener() {
  147.  
  148. @Override
  149. public void onErrorResponse(VolleyError error) {
  150. VolleyLog.d(TAG, "Error: " + error.getMessage());
  151. }
  152. });
  153.  
  154. // Adding request to volley request queue
  155. AppController.getInstance().addToRequestQueue(jsonReq);
  156. }
  157. }
  158.  
  159. // Other Four Pages
  160. else {
  161. Cache cache = AppController.getInstance().getRequestQueue().getCache();
  162. Entry entry = cache.get(URL_FEED);
  163. if (entry != null) {
  164. // fetch the data from cache
  165. try {
  166. String data = new String(entry.data, "UTF-8");
  167. try {
  168. parseJsonFeed(new JSONObject(data));
  169. } catch (JSONException e) {
  170. e.printStackTrace();
  171. }
  172. } catch (UnsupportedEncodingException e) {
  173. e.printStackTrace();
  174. }
  175.  
  176. } else {
  177. // making fresh volley request and getting json
  178. JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
  179. URL_FEED, null, new Response.Listener<JSONObject>() {
  180.  
  181. @Override
  182. public void onResponse(JSONObject response) {
  183. VolleyLog.d(TAG, "Response: " + response.toString());
  184. if (response != null) {
  185. parseJsonFeed(response);
  186. }
  187. }
  188. }, new Response.ErrorListener() {
  189.  
  190. @Override
  191. public void onErrorResponse(VolleyError error) {
  192. VolleyLog.d(TAG, "Error: " + error.getMessage());
  193. }
  194. });
  195.  
  196. // Adding request to volley request queue
  197. AppController.getInstance().addToRequestQueue(jsonReq);
  198. }
  199.  
  200. }
  201.  
  202. swipeContainer.setColorSchemeColors(android.R.color.holo_blue_bright,
  203. android.R.color.holo_green_light,
  204. android.R.color.holo_orange_light,
  205. android.R.color.holo_red_light);
  206.  
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213. /**
  214. * Parsing json reponse and passing the data to feed view list adapter
  215. * */
  216. public void parseJsonFeed(JSONObject response) {
  217. try {
  218. // String page = getIntent().getExtras().getString("page");
  219. // if (page.equals("1"))
  220. JSONArray feedArray = response.getJSONArray("feed");
  221.  
  222. for (int i = 0; i < feedArray.length(); i++) {
  223. JSONObject feedObj = (JSONObject) feedArray.get(i);
  224.  
  225. final FeedItem item = new FeedItem();
  226. item.setId(feedObj.getInt("id"));
  227. item.setName(feedObj.getString("name"));
  228.  
  229. // Image might be null sometimes
  230. String image = feedObj.isNull("image") ? null : feedObj
  231. .getString("image");
  232. item.setImge(image);
  233. item.setStatus(feedObj.getString("status"));
  234. item.setProfilePic(feedObj.getString("profilePic"));
  235. item.setTimeStamp(feedObj.getString("timeStamp"));
  236.  
  237. // url might be null sometimes
  238. String feedUrl = feedObj.isNull("url") ? null : feedObj
  239. .getString("url");
  240. item.setUrl(feedUrl);
  241.  
  242. feedItems.add(item);
  243. }
  244.  
  245.  
  246. // notify data changes to list adapater
  247.  
  248.  
  249. listAdapter.notifyDataSetChanged();
  250. } catch (JSONException e) {
  251. e.printStackTrace();
  252. }
  253.  
  254. swipeContainer.setRefreshing(false);
  255. }
  256.  
  257.  
  258.  
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement