Advertisement
ioana_martin98

Untitled

Nov 12th, 2020
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. package ro.ase.seminar1;
  2. /* SEMINAR 7
  3. 12.11.2020
  4. JSON: http://movio.biblacad.ro/movies.json --> alte atribute fata de clasa noastra Movie
  5. ListActivity
  6.  
  7. lansam JSONActivity din BNRActivity (Optiunea 1, apoi CLICK pe optiunea 3(
  8. * */
  9.  
  10. import android.app.ListActivity;
  11. import android.app.ProgressDialog;
  12. import android.graphics.Color;
  13. import android.os.AsyncTask;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.widget.ListAdapter;
  19. import android.widget.SimpleAdapter;
  20. import android.widget.TextView;
  21.  
  22. import org.json.JSONArray;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25.  
  26. import java.io.BufferedReader;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.InputStreamReader;
  30. import java.net.HttpURLConnection;
  31. import java.net.MalformedURLException;
  32. import java.net.URL;
  33. import java.util.ArrayList;
  34. import java.util.HashMap;
  35.  
  36. public class JSONActivity extends ListActivity {
  37. //se mapeaza activitatea pe controlul vizual de Lista
  38.  
  39. public static final String TAG_MOVIES = "movies"; //denumirea vectorului
  40. public static final String TAG_ID = "id";
  41. public static final String TAG_TITLE = "title";
  42. public static final String TAG_DURATION = "duration";
  43. public static final String TAG_RELEASE = "release";
  44. //pentru metoda onPreExecute in clasa care extinde AsyncTask
  45. //va disparea in momentul in care se afiseaza rezultatele (onPostExecute)
  46. //vom baga un Thread.sleep() pt ca va dura prea putin
  47. private ProgressDialog pDialog;
  48. //obiectul care tine vectorul de filme (movies extras din fisierul JSON)
  49. JSONArray movies = null;
  50. //pentru stocarea rezultatelor pentru a fi transmisa adaptorului
  51. ArrayList<HashMap<String, String>> movieList;
  52.  
  53. @Override
  54. protected void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. setContentView(R.layout.activity_json);
  57.  
  58. movieList = new ArrayList<>();
  59.  
  60. URL url = null;
  61. try {
  62. url = new URL("http://movio.biblacad.ro/movies.json");
  63. } catch (MalformedURLException e) {
  64. e.printStackTrace();
  65. }
  66.  
  67. GetMovies m = new GetMovies();
  68. m.setOnTaskFinishedEvent(new OnTaskExecutionFinished() {
  69. @Override
  70. public void onTaskFinishedEvent(String result) {
  71. if (pDialog.isShowing()) {
  72. try {
  73. Thread.sleep(3000);
  74. } catch (InterruptedException e) {
  75. e.printStackTrace();
  76. }
  77. pDialog.dismiss();
  78. }
  79.  
  80. ListAdapter adapter = new SimpleAdapter(JSONActivity.this, movieList, R.layout.list_item,
  81. new String[]{TAG_TITLE, TAG_RELEASE, TAG_DURATION},
  82. new int[]{R.id.title, R.id.release, R.id.duration}) {
  83. //in loc de { putem pune ; daca nu vrem colorarea duratei filmului cu rosu/verde
  84. //pentru asta folosim getView de fapt
  85. @Override
  86. public View getView(int position, View convertView, ViewGroup parent) {
  87. View view = super.getView(position, convertView, parent);
  88.  
  89. HashMap<String,String> currentRow = movieList.get(position);
  90.  
  91. TextView duration = view.findViewById(R.id.duration);
  92. int valDuration = Integer.parseInt(currentRow.get(TAG_DURATION));
  93. if (valDuration<100)
  94. duration.setTextColor(Color.RED);
  95. else
  96. duration.setTextColor(Color.GREEN);
  97. return view;
  98. }
  99. };
  100. //activitatea e ListActivity deci vede direct setListAdapter
  101. setListAdapter(adapter);
  102. }
  103. });
  104. //din GetMovies care extinde AsyncTask se apeleaza execute(url) ca sa pot lansa procesul
  105. m.execute(url);
  106. }
  107.  
  108. //cream o interfata care va fi un fel de Notifier
  109. public interface OnTaskExecutionFinished {
  110. void onTaskFinishedEvent(String result);
  111. }
  112.  
  113. public class GetMovies extends AsyncTask<URL, Void, String> {
  114.  
  115. private OnTaskExecutionFinished event;
  116.  
  117. public void setOnTaskFinishedEvent(OnTaskExecutionFinished event) {
  118. if (event != null)
  119. this.event = event;
  120. }
  121.  
  122. @Override
  123. protected String doInBackground(URL... urls) {
  124. //copiem din Network
  125. HttpURLConnection conn = null;
  126.  
  127. try {
  128. conn = (HttpURLConnection) urls[0].openConnection();
  129. conn.setRequestMethod("GET");
  130. InputStream ist = conn.getInputStream();
  131.  
  132. InputStreamReader isr = new InputStreamReader(ist);
  133. BufferedReader br = new BufferedReader(isr);
  134. String linie = null;
  135. String sbuf = "";
  136. while ((linie = br.readLine()) != null) {
  137. sbuf += linie;
  138. }
  139. //parsare JSON
  140. loadJSON(sbuf);
  141. return sbuf;
  142.  
  143. } catch (IOException e) {
  144. e.printStackTrace();
  145. }
  146.  
  147. return null;
  148. }
  149.  
  150. @Override
  151. protected void onPostExecute(String s) {
  152. super.onPostExecute(s);
  153. if (this.event != null)
  154. this.event.onTaskFinishedEvent(s);
  155. else
  156. Log.e("GetMovies", "event is null");
  157. }
  158.  
  159. @Override
  160. protected void onPreExecute() {
  161. super.onPreExecute();
  162. pDialog = new ProgressDialog(JSONActivity.this);
  163. pDialog.setMessage("Please wait...");
  164. pDialog.setCancelable(false);
  165. pDialog.show();
  166. }
  167.  
  168. public void loadJSON(String jsonStr) {
  169. if (jsonStr != null) {
  170. JSONObject jsonObject = null;
  171. try {
  172. jsonObject = new JSONObject(jsonStr);
  173. } catch (JSONException e) {
  174. e.printStackTrace();
  175. }
  176. //initializam JSONArray ul
  177. try {
  178. movies = jsonObject.getJSONArray(TAG_MOVIES);
  179. for (int i = 0; i < movies.length(); i++) {
  180. JSONObject c = movies.getJSONObject(i);
  181. String id = c.getString(TAG_ID);
  182. String title = c.getString(TAG_TITLE);
  183. String duration = c.getString(TAG_DURATION);
  184. String release = c.getString(TAG_RELEASE);
  185.  
  186. //popularea listei de obiecte Film, asociem cheile cu valorile
  187. //poate fi inlocuit cu crearea unui obiect din clasa Movie
  188. HashMap<String, String> movie = new HashMap<>();
  189. movie.put(TAG_ID, id);
  190. movie.put(TAG_TITLE, title);
  191. movie.put(TAG_DURATION, duration);
  192. movie.put(TAG_RELEASE, release);
  193.  
  194. movieList.add(movie);
  195. }
  196. } catch (JSONException e) {
  197. e.printStackTrace();
  198. }
  199. } else
  200. Log.e("loadJSON", "JSON object is null");
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement