Hi everyone, I am developing a simple app for learning purposes, which uses the movie db APIs. The code i am using to perform http requests to themoviedb.org is the following:
HttpURLConnection connection;
try {
/* starting http request */
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
/* reading the response */
BufferedReader rd;
if(connection.getResponseCode() != 200){
rd = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
} else {
rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
StringBuilder content = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
content.append(line).append('\n');
}
/* returning it to the caller */
return content.toString();
} catch (IOException e){
e.printStackTrace();
Log.v(TAG, "Problems connecting to the server: " + e.getMessage());
return "";
}
This code is working perfectly to retrieve a list of movies, with a url like:
http://api.themoviedb.org/3/movie/popular?api_key=[your api key]&page=1
But to retrieve the list of videos for a movie, so a url like:
http://api.themoviedb.org/3/movie/269149/videos?api_key=[your api key]
is not working! getResponseCode() returns to me 404 and in the error stream i read:
{“status_code”:34,“status_message”:“The resource you requested could not be found.”}
The odd thing is that if with the debugger i copy the url the code is working on and copy it on my browser it works as expected (showing me the json response).
Any chance someone can help me with this issue?
Can't find a movie or TV show? Login to create it.
Want to rate or add this item to a list?
Not a member?
Reply by wellsaid2
on March 13, 2018 at 8:58 PM
I solved it. It was the connection.setDoOutput(true)
Reply by Travis Bell
on March 14, 2018 at 4:28 AM
Hi @wellsaid2, happy to hear it. Hopefully that can help other people in the future!