diff --git a/src/main/java/com/example/cosmos_serversb/entities/Shop.java b/src/main/java/com/example/cosmos_serversb/entities/Shop.java index 11b6c42..15fa9e7 100644 --- a/src/main/java/com/example/cosmos_serversb/entities/Shop.java +++ b/src/main/java/com/example/cosmos_serversb/entities/Shop.java @@ -13,53 +13,59 @@ /** * SHOP用のクラス * - * @param longitude string - * @param latitude String - * @param longitudeRange String - * @param latitudeRange String - * @param shop String + * @param genre string + * @param code int * */ @JsonInclude(JsonInclude.Include.NON_NULL) - public String longitude; @JsonView(ShopsView.getShopsView.class) - public String latitude; + public String genre; @JsonView(ShopsView.getShopsView.class) - public String longitudeRange; - public String latitudeRange; - public String shop; - - public Set shops = new HashSet<>(); + public int code; + @JsonView(ShopsView.getShopsView.class) + public double latitude; + @JsonView(ShopsView.getShopsView.class) + public double longitude; public Shop() { } - public Shop(String longitude, String latitude, String longitudeRange, String latitudeRange, String shop) { - setlongitude(longitude); - setlatitude(latitude); - setlongitudeRange(longitudeRange); - setlatitudeRange(latitudeRange); - setshop(shop); + public Shop(String genre, int code, double latitude, double longitude) { + setGenre(genre); + setCode(code); + setLatitude(latitude); + setLongitude(longitude); } - public void setlongitude(String longitude) { - this.longitude = longitude; + public void setGenre(String genre) { this.genre = genre; } - public void setlatitude(String latitude) { + public String getGenre(){ + return genre; + } + + public void setCode(int code) { + this.code = code; + } + + public int getCode(){ + return code; + } + + public void setLatitude(double latitude) { this.latitude = latitude; } - public void setlongitudeRange(String longitudeRange) { - this.longitudeRange = longitudeRange; + public double getLatitude() { + return latitude; } - public void setlatitudeRange(String latitudeRange) { - this.latitudeRange = latitudeRange; + public void setLongitude(double longitude) { + this.longitude = longitude; } - public void setshop(String shop) { - this.shop = shop; + public double getLongitude() { + return longitude; } } \ No newline at end of file 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 cc26d93..e360265 100644 --- a/src/main/java/com/example/cosmos_serversb/models/Shops.java +++ b/src/main/java/com/example/cosmos_serversb/models/Shops.java @@ -2,21 +2,30 @@ import com.example.common.LogUtils; import com.example.cosmos_serversb.entities.*; -import org.hibernate.Session; -import org.hibernate.SessionFactory; +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 org.hibernate.Session; +//import org.hibernate.SessionFactory; import javax.inject.Singleton; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; @Singleton public class Shops { private static Shops theInstance = null; - private static SessionFactory sessionFactory; + //private static SessionFactory sessionFactory; + private static ArrayList shops = new ArrayList<>(); private static String baseURI="http://nitta-lab-www.is.konan-u.ac.jp/"; private static String AppName="cosmos"; private Shops() { - sessionFactory = SessionFactoryManager.getInstance().getSessionFactory(); + //sessionFactory = SessionFactoryManager.getInstance().getSessionFactory(); } public static Shops getInstance() { @@ -26,12 +35,45 @@ return theInstance; } - public static Shop getShop(String longitude, String latitude, String longitudeRange, String latitudeRange, String shop) { - String uri = baseURI + AppName +shop; + public static ArrayList getShop(String leftLatitude, String leftLongitude, String rightLatitude, String rightLongitude) { + String result = ""; + String genre; + String code; + String location; + String[] split; + try { + URL url = new URL("https://map.yahooapis.jp/search/local/V1/localSearch?bbox=" + leftLatitude + "," + leftLongitude + "," + rightLatitude + "," + rightLongitude + "&gc=02&results=90&output=json&appid=dj00aiZpPTVzYzloUDJjS0VMSyZzPWNvbnN1bWVyc2VjcmV0Jng9MGE-"); + 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(); + } + JsonNode jnode = null; + try{ + JsonFactory jfactory = new JsonFactory(); + JsonParser parser = jfactory.createJsonParser(result); + ObjectMapper mapper = new ObjectMapper(); + jnode = mapper.readTree(parser); + }catch(Exception e){ + e.printStackTrace(); + } - Session session = SessionManager.getInstance().getSession(); - Shop testShop = new Shop(longitude,latitude,longitudeRange,latitudeRange,shop); - session.save(testShop); - return testShop; + for (int i = 0; i < 10; i++) { + genre = jnode.get("Feature").get(i).get("Property").get("Genre").get(0).get("Name").asText(); + code = jnode.get("Feature").get(i).get("Property").get("Genre").get(0).get("Code").asText(); + location = jnode.get("Feature").get(i).get("Geometry").get("Coordinates").asText(); + split = location.split(","); + Shop shop = new Shop(genre, Integer.parseInt(code), Double.parseDouble(split[0]), Double.parseDouble(split[1])); + shops.add(shop); + } + + return shops; } } \ No newline at end of file diff --git a/src/main/java/com/example/cosmos_serversb/resources/ShopsRest.java b/src/main/java/com/example/cosmos_serversb/resources/ShopsRest.java index 98fb8a0..2249fa2 100644 --- a/src/main/java/com/example/cosmos_serversb/resources/ShopsRest.java +++ b/src/main/java/com/example/cosmos_serversb/resources/ShopsRest.java @@ -17,15 +17,15 @@ public class ShopsRest { @GET - public String getShopsInfo(@QueryParam("longitude") String longitude, - @QueryParam("latitude") String latitude, - @QueryParam("longitudeRange") String longitudeRange, - @QueryParam("latitudeRange") String latitudeRange, - @QueryParam("shop") String shop)throws JsonProcessingException { - SessionManager.getInstance().getSession(); + public String getShopsInfo(@QueryParam("leftLatitude") String leftLatitude, + @QueryParam("leftLongitude") String leftLongitude, + @QueryParam("rightLatitude") String rightLatitude, + @QueryParam("rightLongitude") String rightLongitude + )throws JsonProcessingException { + //SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writerWithView(ShopsView.getShopsView.class).writeValueAsString(Shops.getInstance().getShop(longitude,latitude,longitudeRange,latitudeRange,shop)); - SessionManager.getInstance().closeSession(); + String json = mapper.writerWithView(ShopsView.getShopsView.class).writeValueAsString(Shops.getInstance().getShop(leftLatitude,leftLongitude,rightLatitude,rightLongitude)); + //SessionManager.getInstance().closeSession(); LogUtils.info("位置情報を取得"); return json; } diff --git a/src/test/java/HttpUrlConnectionTest/SampleTest.java b/src/test/java/HttpUrlConnectionTest/SampleTest.java new file mode 100644 index 0000000..e8857f6 --- /dev/null +++ b/src/test/java/HttpUrlConnectionTest/SampleTest.java @@ -0,0 +1,68 @@ +package HttpUrlConnectionTest; + +import com.example.cosmos_serversb.models.Groups; +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 com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.node.JsonNodeType; +import jdk.nashorn.internal.ir.ObjectNode; + +import java.io.*; +import java.net.*; + +public class SampleTest { + 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; + } + + public static void main(String[] args) { + String Result; + JsonNode json; + String URL = "https://map.yahooapis.jp/search/local/V1/localSearch?bbox=139.727685,35.663782,139.734251,35.669378&gc=02&results=90&output=json&appid=dj00aiZpPTVzYzloUDJjS0VMSyZzPWNvbnN1bWVyc2VjcmV0Jng9MGE-"; + Result = getResult(URL); + json = getJsonNode(Result); + + for (int i = 0; i < 90; i++) { + String genre = json.get("Feature").get(i).get("Property").get("Genre").get(0).get("Name").asText(); + String code = json.get("Feature").get(i).get("Property").get("Genre").get(0).get("Code").asText(); + String location = json.get("Feature").get(i).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); + } +} \ No newline at end of file