Hi, I'll tell you, I have a problem extracting info from an API in an Android app. When I'm parsing the JSON, it says: "No values for movies" in this case.
I'll stick the code to see if you can help me out. The fact is that this same code works well for other API's with the same structure, so I'm getting a little crazy. I've also been looking at other stack posts in both English and Spanish but none of them have helped me.
Here the code:
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);
}
Here the debugger log:
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 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. I have fears for the future of the world with such a reckless power left ungoverne "
e = {JSONException @ 4508} "org.json.JSONException: No value for movies"