From d3ea1b1956c863bf15688c491e0ac1eccd30a678 Mon Sep 17 00:00:00 2001 From: Jannik Date: Thu, 19 Apr 2018 11:45:11 +0200 Subject: [PATCH] fixed api arror if title dosent exist at omdbAPI --- .../controller/OMDbAPIController.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/kellerkinder/HomeFlix/controller/OMDbAPIController.java b/src/main/java/kellerkinder/HomeFlix/controller/OMDbAPIController.java index db0ee80..0a77f19 100644 --- a/src/main/java/kellerkinder/HomeFlix/controller/OMDbAPIController.java +++ b/src/main/java/kellerkinder/HomeFlix/controller/OMDbAPIController.java @@ -61,7 +61,7 @@ public class OMDbAPIController implements Runnable { String output = null; String posterPath = null; - // get by title, TODO implement search FIXME set correct info if film dosen't exist + // get information by title try { URL apiUrl = new URL(URL + mainWindowController.getOmdbAPIKey() + "&t=" + mainWindowController.getCurrentTitle().replace(" ", "%20")); @@ -78,6 +78,35 @@ public class OMDbAPIController implements Runnable { JsonObject object = Json.parse(output).asObject(); + if (object.getString("Error", "").equals("Movie not found!")) { + // if the movie was not found try to search it + // TODO split the name intelligent as it may contain the film title + // query the api + try { + URL apiUrl = new URL(URL + mainWindowController.getOmdbAPIKey() + "&s=" + + mainWindowController.getCurrentTitle().replace(" ", "%20")); + BufferedReader ina = new BufferedReader(new InputStreamReader(apiUrl.openStream())); + output = ina.readLine(); + ina.close(); + System.out.println(apiUrl); + LOGGER.info("response from search " + URL + " was valid"); + LOGGER.info(output); + } catch (Exception e) { + LOGGER.error("error while making api request or reading response"); + LOGGER.error("response from search" + URL + " was: \n" + output, e); + return; + } + + JsonObject searchObject = Json.parse(output).asObject(); + // TODO new query with the actual title + if (object.getString("Response", "").equals("True")) { + System.out.println(searchObject.getString("Title", "")); + } + + System.out.println("Movie not found, not setting cache"); + return; + } + responseString[0] = object.getString("Title", ""); responseString[1] = object.getString("Year", ""); responseString[2] = object.getString("Rated", "");