Skip to content

Commit

Permalink
[#11] feat : OpneApi를 통해서 공기 대기를 받아오는 기능 구현
Browse files Browse the repository at this point in the history
- OpenApi를 활용하여 오늘의 대기상태를 받아오는 기능을 구현하였습니다.

#11

# 꼬릿말은 아래에 작성 : ex) #이슈 번호

# --COMMIT END--
# <타입> 리스트
# feat : 기능 (새로운 기능)
# fix : 버그 (버그 수정)
# refactor : 리팩토링
# style : 스타일(코드 형식, 세미콜론 추가 : 비지니스 로직 변경 X)
# docs : 문서 (문서 추가, 수정, 삭제)
# test : 테스트 (테스트 코드 추가, 수정, 삭제 : 비지니스 로직 변경 X)
# chroe : 기타 변경사항(빌드 스크립트 수정 등)
-------------------------
#	제목 첫 글자 대문자
#	제목은 명령문
#	제목 끝에 마침표(.) 금지
#	제목과 본문을 한 줄 띄워 분리하기
#	본문은 "어떻게" 보다 "무엇을" , "왜"를 설명한다
#	본문에 여러줄의 메시지를 작성할 땐 "-"로 구분
-------------------------
  • Loading branch information
haveagood committed Mar 31, 2020
1 parent 4e11fc0 commit 045fa35
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
27 changes: 25 additions & 2 deletions BE/src/main/java/com/codesquad/dust7/DailyDustResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,42 @@ public class DailyDustResult {

public ArrayList<OneHourDust> dailyDustParser(String stationName) throws IOException, JSONException {
OpenApiGetData openApiGetData = new OpenApiGetData();
JSONArray dailyDustData = openApiGetData.getDustInfo(stationName);
JSONArray dailyDustData = openApiGetData.getStationDailyDustInfo(stationName);
ArrayList<OneHourDust> parseDailyDustData = new ArrayList<>();

for (int i = 0; i < dailyDustData.length(); i++) {
JSONObject oneHourDustData = dailyDustData.getJSONObject(i);
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<DailyAirCondition> dailyAirConditionParser() throws IOException, JSONException {
OpenApiGetData openApiGetData = new OpenApiGetData();
JSONArray dailyAirConditionData = openApiGetData.getAirCondition();
ArrayList<DailyAirCondition> 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();
}
}
50 changes: 46 additions & 4 deletions BE/src/main/java/com/codesquad/dust7/OpenApiGetData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")); /*측정소 이름*/
Expand All @@ -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;
Expand Down

0 comments on commit 045fa35

Please sign in to comment.