Hi,
I have a problem where my movie search code is often returning an empty result.
I am iterating through a list of movies in Java as in the below snippet...
This generally works but more often than not returns an empty result.
The below code randomly returns an empty result eg...
Movie search URL: https://api.themoviedb.org/3/search/movie?api_key=3bbe736fb......&query="Pirates Of The Caribbean 1 - The Curse Of The Black Pearl"
Movie search response :<{"page":1,"total_results":0,"total_pages":0,"results":[]}>
Movie JSON data in string format
{"page":1,"total_results":0,"total_pages":0,"results":[]}
Or returns a correct result eg...
Movie search URL: https://api.themoviedb.org/3/search/movie?api_key=3bbe736fb......&query="Gladiator"
Movie search response :<{"page":1,"total_results":41,"total_pages":3,"results": [{"popularity":42.718,"vote_count":11567,"video":false,"poster_path":"\/dvKsQB84W2Sv6s7jpGmzQBVyQe3.jpg","id":98,"adult":false,"backdrop_path":"\/.......
Either way, if I cut/paste the search URL into a browser I get the correct result.
Any ideas or help would be greatly appreciated.
Code snippet
URL url = new URL(searchUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
conn.connect();
int responsecode = conn.getResponseCode();
if(responsecode != 200) {
System.out.println("RESPONSE CODE: "+responsecode);
throw new RuntimeException("HttpResponseCode: " +responsecode);
}
Scanner sc = new Scanner(url.openStream());
while(sc.hasNext()) {
tempLine=sc.nextLine();
System.out.println("Movie search response :<"+tempLine+">");
inLine+=tempLine;
}
sc.close();
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 Knossos
on May 2, 2020 at 9:30 AM
Are you url encoding the title? For example I noticed that "pirates of the caribbean" has spaces in it and "gladiator" does not. Spaces when url encoded are %20. This may work in the browser as the browser may url encode the address before sending the request.
Reply by foxtech
on May 2, 2020 at 9:43 PM
@Knossos , thank you very much for your help. This was exactly my problem and all is working well now...