Skip to main content
5 de 5
adicionou 9 caracteres ao conteúdo
viana
  • 27,7mil
  • 26
  • 96
  • 198

Como posso percorrer este JSON e recuperar a lista de Movies?

Estou com dificuldades em percorrer este JSON e extrair os dados do Array "movies".

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

O erro é este:

java.lang.NullPointerException na linha for (Movies m : catalogoMovies.movies)

Segue um trecho do código:

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());
        }
    });

Log de Erro

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;

}