From 045fa35e357d8ab6825ef37b5d44f8810ed51836 Mon Sep 17 00:00:00 2001 From: "haveagood@naver.com" Date: Tue, 31 Mar 2020 18:43:08 +0900 Subject: [PATCH] =?UTF-8?q?[#11]=20feat=20:=20OpneApi=EB=A5=BC=20=ED=86=B5?= =?UTF-8?q?=ED=95=B4=EC=84=9C=20=EA=B3=B5=EA=B8=B0=20=EB=8C=80=EA=B8=B0?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=9B=EC=95=84=EC=98=A4=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - OpenApi를 활용하여 오늘의 대기상태를 받아오는 기능을 구현하였습니다. #11 # 꼬릿말은 아래에 작성 : ex) #이슈 번호 # --COMMIT END-- # <타입> 리스트 # feat : 기능 (새로운 기능) # fix : 버그 (버그 수정) # refactor : 리팩토링 # style : 스타일(코드 형식, 세미콜론 추가 : 비지니스 로직 변경 X) # docs : 문서 (문서 추가, 수정, 삭제) # test : 테스트 (테스트 코드 추가, 수정, 삭제 : 비지니스 로직 변경 X) # chroe : 기타 변경사항(빌드 스크립트 수정 등) ------------------------- # 제목 첫 글자 대문자 # 제목은 명령문 # 제목 끝에 마침표(.) 금지 # 제목과 본문을 한 줄 띄워 분리하기 # 본문은 "어떻게" 보다 "무엇을" , "왜"를 설명한다 # 본문에 여러줄의 메시지를 작성할 땐 "-"로 구분 ------------------------- --- .../com/codesquad/dust7/DailyDustResult.java | 27 +++++++++- .../com/codesquad/dust7/OpenApiGetData.java | 50 +++++++++++++++++-- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/BE/src/main/java/com/codesquad/dust7/DailyDustResult.java b/BE/src/main/java/com/codesquad/dust7/DailyDustResult.java index 4d036af..edf342c 100644 --- a/BE/src/main/java/com/codesquad/dust7/DailyDustResult.java +++ b/BE/src/main/java/com/codesquad/dust7/DailyDustResult.java @@ -12,7 +12,7 @@ public class DailyDustResult { public ArrayList dailyDustParser(String stationName) throws IOException, JSONException { OpenApiGetData openApiGetData = new OpenApiGetData(); - JSONArray dailyDustData = openApiGetData.getDustInfo(stationName); + JSONArray dailyDustData = openApiGetData.getStationDailyDustInfo(stationName); ArrayList parseDailyDustData = new ArrayList<>(); for (int i = 0; i < dailyDustData.length(); i++) { @@ -20,11 +20,34 @@ public ArrayList dailyDustParser(String stationName) throws IOExcep String dataTime = oneHourDustData.get("dataTime").toString(); String pm10Grade = oneHourDustData.get("pm10Grade").toString(); String pm10Value = oneHourDustData.get("pm10Value").toString(); - OneHourDust oneHourDust = new OneHourDust(dataTime,pm10Grade,pm10Value); + OneHourDust oneHourDust = new OneHourDust(dataTime, pm10Grade, pm10Value); parseDailyDustData.add(oneHourDust); } return parseDailyDustData; } + public ArrayList dailyAirConditionParser() throws IOException, JSONException { + OpenApiGetData openApiGetData = new OpenApiGetData(); + JSONArray dailyAirConditionData = openApiGetData.getAirCondition(); + ArrayList parseDailyAirCondition = new ArrayList<>(); + for (int i = 0; i < dailyAirConditionData.length(); i++) { + JSONObject oneAirData = dailyAirConditionData.getJSONObject(i); + String dataTime = oneAirData.get("dataTime").toString(); + String imageUrl1 = oneAirData.get("imageUrl1").toString(); + String imageUrl2 = oneAirData.get("imageUrl2").toString(); + String imageUrl3 = oneAirData.get("imageUrl3").toString(); + String informGrade = oneAirData.get("informGrade").toString(); + String informCause = oneAirData.get("informCause").toString(); + String informOverall = oneAirData.get("informOverall").toString(); + DailyAirCondition dailyAirCondition = new DailyAirCondition(dataTime,imageUrl1,imageUrl2,imageUrl3,informGrade,informCause,informOverall); + parseDailyAirCondition.add(dailyAirCondition); + } + return parseDailyAirCondition; + } + + public static void main(String[] args) throws JSONException, IOException { + DailyDustResult dailyDustResult = new DailyDustResult(); + dailyDustResult.dailyAirConditionParser(); + } } diff --git a/BE/src/main/java/com/codesquad/dust7/OpenApiGetData.java b/BE/src/main/java/com/codesquad/dust7/OpenApiGetData.java index 41787c8..63ef594 100644 --- a/BE/src/main/java/com/codesquad/dust7/OpenApiGetData.java +++ b/BE/src/main/java/com/codesquad/dust7/OpenApiGetData.java @@ -10,6 +10,7 @@ import org.json.JSONException; import org.json.JSONObject; +import javax.validation.constraints.Null; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -22,9 +23,12 @@ import java.util.stream.Collectors; public class OpenApiGetData { - public JSONArray getDustInfo(String stationName) throws IOException { + public static final String SERVICEKEY = "=afCrvgZOg17BFetVIzUT3QbEQ4f0E4G1fmPGkRUbQwEpAbNgKNWDtydvCFeX7580oiT6FUsuCae398DYvYoN%2BQ%3D%3D"; + + public JSONArray getStationDailyDustInfo(String stationName) throws IOException { + StringBuilder urlBuilder = new StringBuilder("http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getMsrstnAcctoRltmMesureDnsty"); /*URL*/ - urlBuilder.append("?" + URLEncoder.encode("ServiceKey", "UTF-8") + "=afCrvgZOg17BFetVIzUT3QbEQ4f0E4G1fmPGkRUbQwEpAbNgKNWDtydvCFeX7580oiT6FUsuCae398DYvYoN%2BQ%3D%3D"); /*Service Key*/ + urlBuilder.append("?" + URLEncoder.encode("ServiceKey", "UTF-8") + SERVICEKEY); /*Service Key*/ urlBuilder.append("&" + URLEncoder.encode("numOfRows", "UTF-8") + "=" + URLEncoder.encode("24", "UTF-8")); /*한 페이지 결과 수*/ urlBuilder.append("&" + URLEncoder.encode("pageNo", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8")); /*페이지 번호*/ urlBuilder.append("&" + URLEncoder.encode("stationName", "UTF-8") + "=" + URLEncoder.encode(stationName, "UTF-8")); /*측정소 이름*/ @@ -51,10 +55,48 @@ public JSONArray getDustInfo(String stationName) throws IOException { conn.disconnect(); String result = sb.toString(); - try{ + try { + JSONObject json = new JSONObject(sb.toString()); + return (JSONArray) json.get("list"); + } catch (JSONException e) { + System.out.println("json이 아닙니다.!!!"); + } + return null; + } + + public JSONArray getAirCondition() throws IOException { + + StringBuilder urlBuilder = new StringBuilder("http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getMinuDustFrcstDspth"); /*URL*/ + urlBuilder.append("?" + URLEncoder.encode("ServiceKey", "UTF-8") + SERVICEKEY); /*Service Key*/ + urlBuilder.append("&" + URLEncoder.encode("numOfRows", "UTF-8") + "=" + URLEncoder.encode("10", "UTF-8")); /*한 페이지 결과 수 (조회 날짜로 검색 시 사용 안함)*/ + urlBuilder.append("&" + URLEncoder.encode("pageNo", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8")); /*페이지 번호(조회 날짜로 검색 시 사용 안함)*/ + urlBuilder.append("&" + URLEncoder.encode("searchDate", "UTF-8") + "=" + URLEncoder.encode("2020-03-30", "UTF-8")); /*통보시간 검색 (조회 날짜 입력 없을 경우 한달동안 예보통보 발령 날짜의 리스트 정보를 확인)*/ + urlBuilder.append("&" + URLEncoder.encode("InformCode", "UTF-8") + "=" + URLEncoder.encode("PM10", "UTF-8")); /*통보코드검색 (PM10 : 미세먼지 PM25 : 초미세먼지 O3 : 오존)*/ + urlBuilder.append("&" + URLEncoder.encode("_returnType", "UTF-8") + "=" + URLEncoder.encode("json", "UTF-8")); + URL url = new URL(urlBuilder.toString()); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setRequestProperty("Content-type", "application/json"); + System.out.println("Response code: " + conn.getResponseCode()); + BufferedReader rd; + if (conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) { + rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); + } else { + rd = new BufferedReader(new InputStreamReader(conn.getErrorStream())); + } + StringBuilder sb = new StringBuilder(); + String line; + while ((line = rd.readLine()) != null) { + sb.append(line); + } + rd.close(); + conn.disconnect(); + + String result = sb.toString(); + try { JSONObject json = new JSONObject(sb.toString()); return (JSONArray) json.get("list"); - }catch (JSONException e){ + } catch (JSONException e) { System.out.println("json이 아닙니다.!!!"); } return null;