3

Buenas, os cuento, tengo un problema al extraer info de una API en una app de Android. Cuando estoy parseando el JSON me dice: "No values for movies" en este caso.

Os pego el código a ver si me podéis echar una mano. El caso es que este mismo código me funciona bien para otras API's con la misma estructura, así que me estoy volviendo un poco loco. También he estado mirando otros post de stack tanto en inglés como en español pero ninguna me ha servido.

Aquí el código:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recommendations);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    RequestQueue queue = Volley.newRequestQueue(this);
    String url ="http://www.myapifilms.com/imdb/search?" +
            "token=apiKey&format=json&count=5";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
            url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    //JSONObject result = new JSONObject(response);
                    JSONArray arrayResults = response.getJSONArray("movies");
                    for(int i=0;i<arrayResults.length();i++) {
                        JSONObject object = arrayResults.getJSONObject(i);
                        String title = object.getString("title");
                        Log.v("TITULO: ", title);
                        String year = object.getString("year");
                        String runtime = object.getString("runtime");
                        String urlPoster = object.getString("urlPoster");
                        Log.v("POSTER: ", urlPoster);
                        String simplePlot = object.getString("simplePlot");
                        String rating = object.getString("rating");
                        items.add(new Movies(title, year, runtime, urlPoster, simplePlot, rating));
                        adapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(Recommendations.this, "JSON Error", Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(Recommendations.this, "Volley Error", Toast.LENGTH_SHORT).show();
            }
        });
    queue.add(request);

    recycler = (RecyclerView) findViewById(R.id.reciclador);
    recycler.setHasFixedSize(true);

    lManager = new LinearLayoutManager(this);
    recycler.setLayoutManager(lManager);
    adapter = new MoviesAdapter(items);
    recycler.setAdapter(adapter);
}

Aquí el log del debugger:

response = {JSONObject@4498} "{"data":{"movies":[{"title":"Batman v Superman: Dawn of Justice","originalTitle":"","year":"2016","releaseDate":"20160325","directors":[{"name":"Zack Snyder","id":"nm0811583"}],"writers":[{"name":"Chris Terrio","id":"nm0006516"},{"name":"David S. Goyer","id":"nm0333060"}],"runtime":"151 min","urlPoster":"http://ia.media-imdb.com/images/M/MV5BNTE5NzU3MTYzOF5BMl5BanBnXkFtZTgwNTM5NjQxODE@._V1_UY268_CR0,0,182,268_AL_.jpg","countries":["USA"],"languages":["English"],"genres":["Action","Adventure","Fantasy","Sci-Fi"],"plot":"Following his titanic struggle against General Zod, Metropolis has been razed to the ground and Superman is the most controversial figure in the world. While for many he is still an emblem of hope, a growing number of people consider him a threat to humanity, seeking justice for the chaos he has brought to Earth. As far as Bruce Wayne is concerned, Superman is clearly a danger to society. He fears for the future of the world with such a reckless power left ungoverne"

e = {JSONException@4508} "org.json.JSONException: No value for movies"

2
  • ¿Puedes pasar el JSON esperado?
    – Ipman1971
    el 12 abr. 2016 a las 12:24
  • Ipman1971, en el log del debugger aparece un fragmento del JSON, no obstante ya caí ayer en la solución. ¡Muchas gracias de todas formas! el 13 abr. 2016 a las 14:30

1 respuesta 1

3

Vale, solucionado, me estaba saltando el objeto data:

try{
    //AQUÍ he añadido este código y listo.
    JSONObject data = response.getJSONObject("data");
    JSONArray arrayResults = response.getJSONArray("movies");
    for(int i=0;i<arrayResults.length();i++) {
    ...
...

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.