-2

I need to loop through a JSON and get the following information:

The original title, overview, release date, poster_path.

I have the following code..

 @Override
    protected void onPostExecute(String s) {


        try {

            JSONObject jsonObject = new JSONObject(result);

            JSONArray jArray = jsonObject.getJSONArray("results");

            JSONObject json_data = null;



          for (int i = 0; i < jArray.length(); i++) {


             JSONObject finalObject = jArray.getJSONObject(i);

            String movie_title = finalObject.getString("title");

            String releaseDate = finalObject.getString("release_date");



                mMovie_title.setText(movie_title);
                mReleaseDate.setText(releaseDate);



           }


            super.onPostExecute(s);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

The problem with this code is that it only gets me the final movie in the array.

It does not print any other movie titles out.

What I need to do is fetch the movie titles from the JSON along with the overview, release dates & poster path.

Here is the JSON.. https://api.themoviedb.org/3/movie/top_rated?api_key=f1d314280284e94ff7d1feeed7d44fdf&language=en-US&page=1

Any help is much appreciated.

sg82013
  • 31
  • 4

3 Answers3

1
mMovie_title.setText(movie_title);
mReleaseDate.setText(releaseDate);

It looks like you are re-assigning the same variable everytime. That's why you only get the last element.

You should probably have a StringBuilder

StringBuilder titles = new StringBuilder();

for ... {
    ...
    titles.append(movie_title);
}

mMovie_title.setText(titles.toString());
donkon
  • 909
  • 9
  • 23
0

It's not a good decision to loop the JSON.

Try to use GSON The performance is better than that and also it's a very good practice

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

Nice coding!

Faustino Gagneten
  • 2,564
  • 2
  • 28
  • 54
0

If you want to store multiple movie titles then you need a few things.

First, you need a movie object to hold the values of the movie titles and the movie dates. Something like:

public class Movie {
    private String movieTitle;
    private String releaseDate;
    public Movie(String movieTitle, String releaseDate) {
        super();
        this.movieTitle = movieTitle;
        this.releaseDate = releaseDate;
    }
}

Then you need to have a list of movies.

super.onPostExecute(s);
List<Movie> movies = new ArrayList<Movie>();
Movie movie = null;
try {
    JSONObject jsonObject = new JSONObject(result);
    JSONArray jArray = jsonObject.getJSONArray("results");
    JSONObject json_data = null;
        for (int i = 0; i < jArray.length(); i++) 
            JSONObject finalObject = jArray.getJSONObject(i);
            String movie_title = finalObject.getString("title");
            String releaseDate = finalObject.getString("release_date");
            movies.add(new Movie(movie_title, releaseDate));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    // Do something with movies
    // example ::
    for (Movie movie : movies) {
        textView.setText(movie.getMovieTitle); // etc...
    }
anomeric
  • 688
  • 5
  • 17