How can I browse this JSON and recover the Movies list?

Asked

Viewed 489 times

2

I am having difficulty going through this JSON and extracting the data from the "Movies Array".

Follows JSON: (https://yts.ag/api/v2/list_movies.json)

The mistake is this:

java.lang.Nullpointerexception for (Movies m : catalogoMovies.Movies)

Follows an excerpt from the code:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(MovieService.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    MovieService service = retrofit.create(MovieService.class);
    final Call<CatalogoMovies> requestCatalogo = service.listCatalog();

    requestCatalogo.enqueue(new Callback<CatalogoMovies>() {
        @Override
        public void onResponse(Call<CatalogoMovies> call, Response<CatalogoMovies> response) {
            if (!response.isSuccessful()) {
                Log.e(TAG, "Error: " + response.code());
            } else {
                CatalogoMovies catalogoMovies = response.body();
                for (Movies m : catalogoMovies.movies) {
                    Log.e(TAG, "TITLE: " + m.title);
                }
            }
        }

        @Override
        public void onFailure(Call<CatalogoMovies> call, Throwable t) {
            Log.e(TAG, "Error: " + t.getMessage());
        }
    });

Error log

FATAL EXCEPTION: main
   Process: com.example.luciano.saiufilme, PID: 12116
   java.lang.NullPointerException
       at com.example.luciano.saiufilme.HomeActivity$1.onResponse(HomeActivity.java:40)
       at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5017)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
       at dalvik.system.NativeStart.main(Native Method)

Catalogomovies

public class CatalogoMovies {

     public List<Movies> movies;

}
  • You have set up internet access permission ? documentation

  • Yes, I’ve already added this permission on Androidmanifest.

  • edits the question and puts the generated error log.

  • I edited it. As I said at the beginning of the post, the error is java.lang.Nullpointerexception in the for line (Movies m : catalogo.Movies) {

  • See if you can help guj.com.br

  • How is your object CatalogoMovies ?

  • You could put in your question the contents of Catalogomovies?

  • Take a look, I’ve already edited.

Show 3 more comments

1 answer

2

This NullPointerException occurs because within CatalogoMovies there is a List of Movie!

The structure of Objects is different from the JSON that you added to link.

The List is within data:

{ // OBJETO CatalogoMovies
    "status": "ok",
    "status_message": "Query was successful",
    "data": {            // OBEJTO DATA
        "movie_count": 6334,
        "limit": 20,
        "page_number": 1,
        "movies": [{     // AQUI ESTÁ A LISTA
......

To make it easier (and if you’re using Android Studio), I suggest using a plugin to generate these classes automatically.

To install: Top menu:

File> Settings

In the Window that will be displayed, look in the side menu:

Plugins

Search for: Json2pojo

Install the plugin, and restart Android Studio!

Once this is done, copy the Json complete, right-click on the package that will add the classes,and go to

New> Generate POJO from JSON

inserir a descrição da imagem aqui

Paste your JSON and enter a name for class: inserir a descrição da imagem aqui

Obs.: If an error occurs with the tag @Generated("net.hexar.json2pojo") remove the without problems.

This plugin will generate the classes automatically, avoiding this type of error.

PLUGIN LINK

LINK ABOUT INSTALL PLUGIN ANDROID STUDIO

  • Hi Thiago, I did what you recommended. But now the error is another: ERROR: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

  • It is exactly the same JSON?

  • use this address to validate the return https://jsonlint.com/

  • Yes, exactly the same.

  • Strange, he says that instead of starting a list, he’s starting an object! I did a test with the link here and it worked! Can you edit the question and put how you are doing now? if possible add the object that has the data tbm!

  • Hi Thiago, I’ll give you the app link on Github. I’m not sure how to fix this yet, rsrs https://github.com/luciano01/AppFilmes

  • instead of Call<List<Catalogomovies>> put Call<Catalogomovies>

  • I made this change, after recovering the body of this JSON, how should I proceed to get the data? I’m doing so, but it’s wrong. ;CatalogoMovies catalogoMovies = (CatalogoMovies) response.body();&#xA; for(Data data : catalogoMovies.getData()){&#xA; for(Movie movie : data.getMovies()){&#xA; Log.i(TAG, "ERROR: " + movie.getTitle());&#xA; }&#xA; }

  • UFA! I got it. The code snippet in my last comment was wrong. Correct code: 'Catalogomovies catalogoMovies = Response.body(); for(Movie data : catalogoMovies.getmData().getMovies(){ Log. i(TAG, "TITLE: " + data.getTitle()); }'

  • Thiago, thank you so much for your help. Thanks to your patience and collaboration I managed to solve my problem. I thank you immensely. Hug.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.