Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. private class Cast extends AsyncTask<Object, Void, ArrayList<String>> {
  2. ProgressDialog dialog;
  3.  
  4. @Override
  5. protected void onPreExecute() {
  6. // TODO Auto-generated method stub
  7. super.onPreExecute();
  8. dialog=new ProgressDialog(Detailed_View.this);
  9. dialog.setMessage("Loading, please wait");
  10. dialog.setTitle("Connecting server");
  11. dialog.show();
  12. dialog.setCancelable(false);
  13.  
  14.  
  15. }
  16.  
  17. private final String KeY = "";
  18. private static final String DEBUG_TAG = "TM";
  19.  
  20. @Override
  21. protected ArrayList<String> doInBackground(Object... params) {
  22. try {
  23. return getCast();
  24. } catch (IOException e) {
  25. return null;
  26. }
  27. }
  28.  
  29. @Override
  30. protected void onPostExecute(ArrayList<String> results_Cast) {
  31. updateListOfCast(results_Cast);
  32. dialog.cancel();
  33.  
  34.  
  35. };
  36.  
  37. public ArrayList<String> getCast() throws IOException {
  38.  
  39. StringBuilder stringBuilder = new StringBuilder();
  40. stringBuilder.append("https://themovie/3/movie/" + id
  41. + "/credit");
  42. stringBuilder.append("?key=" + key);
  43. URL url = new URL(stringBuilder.toString());
  44. // Log.d("urlstring",stringBuilder.toString() );
  45.  
  46. InputStream stream = null;
  47. try {
  48. // Establish a connection
  49. HttpURLConnection conn = (HttpURLConnection) url
  50. .openConnection();
  51. conn.setReadTimeout(10000 /* milliseconds */);
  52. conn.setConnectTimeout(15000 /* milliseconds */);
  53. conn.setRequestMethod("GET");
  54. conn.addRequestProperty("Accept", "application/json"); // Required
  55. // to
  56. // get
  57. // TMDB
  58. // to
  59. // play
  60. // nicely.
  61. conn.setDoInput(true);
  62. conn.connect();
  63.  
  64. int responseCode = conn.getResponseCode();
  65. Log.d(DEBUG_TAG, "The response code is: " + responseCode + " "
  66. + conn.getResponseMessage());
  67.  
  68. stream = conn.getInputStream();
  69. return parseCast(stringify(stream));
  70. } finally {
  71. if (stream != null) {
  72. stream.close();
  73. }
  74. }
  75. }
  76.  
  77. private ArrayList<String> parseCast(String result) {
  78. String streamAsString = result;
  79.  
  80. ArrayList<String> results_Cast = new ArrayList<String>();
  81. try {
  82. JSONObject jsonObject = new JSONObject(streamAsString);
  83. JSONArray array = (JSONArray) jsonObject.get("cast");
  84. Log.d("array view", array.toString());
  85. for (int i = 0; i < array.length(); i++) {
  86. HashMap<String, String> map = new HashMap<String, String>();
  87. JSONObject jsonMovieObject = array.getJSONObject(i);
  88. results_Cast.add(jsonMovieObject.getString("name"));
  89. ids=jsonMovieObject.getString("id");
  90. results_Cast.add(jsonMovieObject.getString("character"));
  91.  
  92. }
  93. } catch (JSONException e) {
  94. Log.d("e", e.toString());
  95. Log.d(DEBUG_TAG, "Error parsing JSON. String was: "
  96. + streamAsString);
  97. }
  98. // Log.d("resulted", results_Cast.toString());
  99. return results_Cast;
  100. }
  101.  
  102. public String stringify(InputStream stream) throws IOException,
  103. UnsupportedEncodingException {
  104. Reader reader = null;
  105. reader = new InputStreamReader(stream, "UTF-8");
  106. BufferedReader bufferedReader = new BufferedReader(reader);
  107. return bufferedReader.readLine();
  108. }
  109. }
  110.  
  111. ListView listView = (ListView) findViewById(R.id.cast_details);
  112. //Log.d("updateViewWithResults", result.toString());
  113. // Add results to listView.
  114. listView.setAdapter(adapter);
  115. Helper.getListViewSize(listView);
  116. gridAdapter = new GridAdapter(this, R.layout.test_row, result); // here I have issue.
  117. listView.setAdapter(gridAdapter);
  118. listView.setOnItemClickListener(new OnItemClickListener() {
  119.  
  120. @Override
  121. public void onItemClick(AdapterView<?> parent, View view,
  122. int position, long idss) {
  123. // TODO Auto-generated method stub
  124. String name = (String) parent.getItemAtPosition(position);
  125. Toast.makeText(Detailed_View.this, name+"Id"+ids, Toast.LENGTH_SHORT)
  126. .show();
  127.  
  128. }
  129. });
  130.  
  131.  
  132. public class GridAdapter extends BaseAdapter {
  133.  
  134. public GridAdapter(Activity a, int resource, ArrayList<String> result) { // here it displays error as The blank final field itemLists may not have been initialized
  135. layoutInflater = (LayoutInflater) a
  136. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  137. Resource = resource;
  138. results=result;
  139. activity=a;
  140. loader=new ImageLoader(a.getApplicationContext());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement