package HttpUrlConnectionTest; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class SampleTest2 { public static String getResult(String urlString) { String result = ""; try { URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.connect(); String tmp = ""; BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); while ((tmp = in.readLine()) != null) { result += tmp; } in.close(); con.disconnect(); } catch (Exception e) { e.printStackTrace(); } 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 Result; JsonNode json; String URL = "https://map.yahooapis.jp/search/local/V1/localSearch?bbox=" + leftLongitude + "," + leftLatitude + "," + rightLongitude + "," + rightLatitude + "&gc=0203&start=" + start + "&results=100&output=json&appid=dj00aiZpPTVzYzloUDJjS0VMSyZzPWNvbnN1bWVyc2VjcmV0Jng9MGE-"; Result = getResult(URL); json = getJsonNode(Result); return json; } public static void main(String[] args) { JsonNode json; int start = 1; int count = 100; int roop = 1; for(int i = 0;i < roop; i++) { json = yolpConnection("34.8", "135.2", "34.7", "135.3", start); String total = json.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 = json.get("Feature").get(j).get("Property").get("Genre").get(0).get("Name").asText(); String code = json.get("Feature").get(j).get("Property").get("Genre").get(0).get("Code").asText(); String location = json.get("Feature").get(j).get("Geometry").get("Coordinates").asText(); String[] split = location.split(","); System.out.println(genre); System.out.println(Integer.parseInt(code)); System.out.println(split[0]); System.out.println(split[1]); } System.out.println(json); } } }