diff --git a/src/main/java/com/example/cosmos_serversb/models/Shops.java b/src/main/java/com/example/cosmos_serversb/models/Shops.java index 1441aba..35bcdb4 100644 --- a/src/main/java/com/example/cosmos_serversb/models/Shops.java +++ b/src/main/java/com/example/cosmos_serversb/models/Shops.java @@ -19,6 +19,7 @@ public class Shops { private static Shops theInstance = null; + private static ArrayList shops = new ArrayList<>(); //private static SessionFactory sessionFactory; private static String baseURI="http://nitta-lab-www.is.konan-u.ac.jp/"; private static String AppName="cosmos"; @@ -34,12 +35,10 @@ return theInstance; } - public static ArrayList getShop(String leftLatitude, String leftLongitude, String rightLatitude, String rightLongitude) { + public static String getResult(String urlString) { String result = ""; - ArrayList shops = new ArrayList<>(); - int count = 100; try { - URL url = new URL("https://map.yahooapis.jp/search/local/V1/localSearch?bbox=" + leftLongitude + "," + leftLatitude + "," + rightLongitude + "," + rightLatitude + "&gc=02&results=100&output=json&appid=dj00aiZpPTVzYzloUDJjS0VMSyZzPWNvbnN1bWVyc2VjcmV0Jng9MGE-"); + URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.connect(); String tmp = ""; @@ -49,31 +48,75 @@ } in.close(); con.disconnect(); - }catch(Exception e){ + } catch (Exception e) { e.printStackTrace(); } - JsonNode jnode = null; - try{ - JsonFactory jfactory = new JsonFactory(); - JsonParser parser = jfactory.createJsonParser(result); - ObjectMapper mapper = new ObjectMapper(); - jnode = mapper.readTree(parser); - }catch(Exception e){ + return result; + } + + public static JsonNode getJsonNode(String jsonString) { + JsonNode head = null; + try { + JsonFactory jfactory = new JsonFactory(); + JsonParser parser = jfactory.createJsonParser(jsonString); + ObjectMapper mapper = new ObjectMapper(); + head = mapper.readTree(parser); + } catch (Exception e) { e.printStackTrace(); } + return head; + } + + private static JsonNode yolpConnection(String leftLatitude, String leftLongitude, String rightLatitude, String rightLongitude, int start, String gc) { + String Result; + JsonNode json; + String URL = "https://map.yahooapis.jp/search/local/V1/localSearch?bbox=" + leftLongitude + "," + leftLatitude + "," + rightLongitude + "," + rightLatitude + "&gc=" + gc + "&start=" + start + "&results=100&output=json&appid=dj00aiZpPTVzYzloUDJjS0VMSyZzPWNvbnN1bWVyc2VjcmV0Jng9MGE-"; + Result = getResult(URL); + json = getJsonNode(Result); + return json; + } + + private static ArrayList getShopsByCode(String leftLongitude, String leftLatitude, String rightLongitude, String rightLatitude, String gc) { + JsonNode jnode; + int start = 1; + int count = 100; + int roop = 1; + + for(int i = 0; i < roop; i++) { + jnode = yolpConnection(leftLongitude, leftLatitude, rightLongitude, rightLatitude, start, gc); + String total = jnode.get("ResultInfo").get("Total").asText(); + + if (Integer.parseInt(total) - (start - 1) < 100) { + count = Integer.parseInt(total) - (start - 1); + } else { + if(Integer.parseInt(total) % count > 0) { + roop = Integer.parseInt(total) / count + 1; + } else { + roop = Integer.parseInt(total) / count; + } + } + start += 100; + + + for (int j = 0; j < count; j++) { + String genre = jnode.get("Feature").get(j).get("Property").get("Genre").get(0).get("Name").asText(); + String code = jnode.get("Feature").get(j).get("Property").get("Genre").get(0).get("Code").asText(); + String location = jnode.get("Feature").get(j).get("Geometry").get("Coordinates").asText(); + String[] split = location.split(","); + Shop shop = new Shop(genre, Integer.parseInt(code), Double.parseDouble(split[0]), Double.parseDouble(split[1])); + shops.add(shop); + } + } + return shops; + } + + public static ArrayList getShop(String leftLatitude, String leftLongitude, String rightLatitude, String rightLongitude) { + if(shops != null) { + shops.clear(); + } - String total = jnode.get("ResultInfo").get("Total").asText(); - if(Integer.parseInt(total) < 100) { - count = Integer.parseInt(total); - } - for (int i = 0; i < count; i++) { - String genre = jnode.get("Feature").get(i).get("Property").get("Genre").get(0).get("Name").asText(); - String code = jnode.get("Feature").get(i).get("Property").get("Genre").get(0).get("Code").asText(); - String location = jnode.get("Feature").get(i).get("Geometry").get("Coordinates").asText(); - String[] split = location.split(","); - Shop shop = new Shop(genre, Integer.parseInt(code), Double.parseDouble(split[0]), Double.parseDouble(split[1])); - shops.add(shop); - } + shops = getShopsByCode(leftLongitude, leftLatitude, rightLongitude, rightLatitude, "0203"); + shops = getShopsByCode(leftLongitude, leftLatitude, rightLongitude, rightLatitude, "0205"); return shops; }