rohittigga

Untitled

Feb 20th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. package com.guidebook.challenge;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.widget.ListView;
  9.  
  10. import com.android.volley.Request.Method;
  11. import com.android.volley.Response;
  12. import com.android.volley.VolleyError;
  13. import com.android.volley.VolleyLog;
  14. import com.android.volley.toolbox.JsonObjectRequest;
  15. import com.guidebook.challenge.adapter.CustomListAdapter;
  16. import com.guidebook.challenge.app.AppController;
  17. import com.guidebook.challenge.model.Event;
  18.  
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22.  
  23. import java.util.ArrayList;
  24. import java.util.List;
  25.  
  26. public class MainActivity extends Activity {
  27. // Log tag
  28. private static final String TAG = MainActivity.class.getSimpleName();
  29.  
  30. // Movies json url
  31. private static final String url = "https://guidebook.com/service/v2/upcomingGuides/";
  32. private ProgressDialog pDialog;
  33. private List<Event> eventList = new ArrayList<Event>();
  34. private ListView listView;
  35. private CustomListAdapter adapter;
  36.  
  37.  
  38.  
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_main);
  43.  
  44. listView = (ListView) findViewById(R.id.list);
  45. adapter = new CustomListAdapter(this, eventList);
  46. listView.setAdapter(adapter);
  47.  
  48. pDialog = new ProgressDialog(this);
  49. // Showing progress dialog before making http request
  50. pDialog.setMessage("Loading...");
  51. pDialog.show();
  52.  
  53. // changing action bar color
  54. /*getActionBar().setBackgroundDrawable(
  55. new ColorDrawable(Color.parseColor("#1b1b1b")));*/
  56.  
  57. // Creating volley request obj
  58. JsonObjectRequest movieReq = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>(){
  59. @Override
  60. public void onResponse(JSONObject response) {
  61. Log.d(TAG, response.toString());
  62. hidePDialog();
  63. System.out.println("lol");
  64. // Parsing json
  65. for (int i = 0; i < response.length(); i++) {
  66. try {
  67.  
  68. JSONObject obj = response.getJSONObject(String.valueOf(i));
  69. Event event = new Event();
  70. event.setStartDate(obj.getString("startDate"));
  71. Log.e("startDate", obj.getString("startDate"));
  72. event.setIconURL(obj.getString("icon"));
  73. event.setRating(((Number) obj.get("rating"))
  74. .doubleValue());
  75. event.setYear(obj.getInt("releaseYear"));
  76.  
  77. // Genre is json array
  78. JSONArray genreArry = obj.getJSONArray("genre");
  79. ArrayList<String> genre = new ArrayList<String>();
  80. for (int j = 0; j < genreArry.length(); j++) {
  81. genre.add((String) genreArry.get(j));
  82. }
  83. event.setGenre(genre);
  84.  
  85. // adding movie to movies array
  86. eventList.add(event);
  87.  
  88. } catch (JSONException e) {
  89. e.printStackTrace();
  90. }
  91.  
  92. }
  93.  
  94. // notifying list adapter about data changes
  95. // so that it renders the list view with updated data
  96. adapter.notifyDataSetChanged();
  97. }
  98. }, new Response.ErrorListener() {
  99. @Override
  100. public void onErrorResponse(VolleyError error) {
  101. VolleyLog.d(TAG, "Error: " + error.getMessage());
  102. hidePDialog();
  103.  
  104. }
  105. });
  106.  
  107. // Adding request to request queue
  108. AppController.getInstance().addToRequestQueue(movieReq);
  109. }
  110.  
  111. @Override
  112. public void onDestroy() {
  113. super.onDestroy();
  114. hidePDialog();
  115. }
  116.  
  117. private void hidePDialog() {
  118. if (pDialog != null) {
  119. pDialog.dismiss();
  120. pDialog = null;
  121. }
  122. }
  123.  
  124. @Override
  125. public boolean onCreateOptionsMenu(Menu menu) {
  126. // Inflate the menu; this adds items to the action bar if it is present.
  127. getMenuInflater().inflate(R.menu.menu_main, menu);
  128. return true;
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment