FetchMovieTask

public class FetchMovieTask extends AsyncTask<String, Void, String[]> { private final String LOG_TAG = FetchMovieTask.class.getSimpleName(); private String[] getMovieDataFromJson(String movieJsonStr) throws JSONException { final String OWN_ID = "id"; final String OWM_RESULTS = "results"; final String OWM_OVERVIEW = "overview"; final String OWM_RELEASE_DATE = "release_date"; final String OWM_POSTER_PATH = "poster_path"; final String OWM_TITLE = "title"; final String OWN_VOTE_AVERAGE = "vote_average"; ArrayList<Movie> movies = new ArrayList<Movie>(); JSONObject movieJson = new JSONObject(movieJsonStr); // Getting JSON Array node JSONArray movieArray = movieJson.getJSONArray(OWM_RESULTS); // looping through All Contacts //getting the poster path for movies String[] mPosterPath = new String[movieArray.length()]; for (int i = 0; i < movieArray.length(); i++) { JSONObject c = movieArray.getJSONObject(i); mPosterPath[i] = movieArray.getJSONObject(i).getString(OWM_POSTER_PATH); Movie movie = new Movie(c.getInt(OWN_ID), c.getDouble(OWN_VOTE_AVERAGE), c.getString(OWM_POSTER_PATH), c.getString(OWM_OVERVIEW), c.getString(OWM_TITLE), c.getString(OWM_RELEASE_DATE)); movies.add(movie); } for(String res : mPosterPath) Log.v(OWM_POSTER_PATH ,res); return mPosterPath; } @Override protected String[] doInBackground(String... params) { // These two need to be declared outside the try/catch // so that they can be closed in the finally block. HttpURLConnection urlConnection = null; BufferedReader reader = null; String movieJsonStr = null; try { String baseUrl = "http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc"; String apiKey = "&api_key=" + BuildConfig.OPEN_MOVIE_DATA_BASE_API_KEY; URL url = new URL(baseUrl.concat(apiKey)); // Create the request to moviedb, and open the connection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); // Read the input stream into a String InputStream inputStream = urlConnection.getInputStream(); StringBuffer buffer = new StringBuffer(); if (inputStream == null) { // Nothing to do. return null; } reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) // But it does make debugging a *lot* easier if you print out the completed // buffer for debugging. buffer.append(line + "\n"); } if (buffer.length() == 0) { // Stream was empty. No point in parsing. return null; } movieJsonStr = buffer.toString(); } catch (IOException e) { Log.e(LOG_TAG, "Error ", e); // If the code didn't successfully get the weather data, there's no point in attemping // to parse it. return null; } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (reader != null) { try { reader.close(); } catch (final IOException e) { Log.e(LOG_TAG, "Error closing stream", e); } } } try { return getMovieDataFromJson(movieJsonStr); } catch (JSONException e) { e.printStackTrace(); } return null; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.