1. 공공 데이터 포털 둘러보기
공공데이터(Public Data)는 정부, 공공기관, 지방자치단체 등이 법령에 따라 생성, 수집, 관리하는 데이터로서, 누구나 자유롭게 이용할 수 있도록 공개된 데이터를 의마한다.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F21273b41-e32a-48ff-9a50-84cdf46b6ace%252FUntitled.png%3Ftable%3Dblock%26id%3D49984dfb-53cc-451b-bb74-d2f3f14278bf%26cache%3Dv2&w=3840&q=75)
공공데이터 포털에서 기상청_단기예보 API 를 선택, 활용신청을 선택한.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252Fb473c2eb-6690-4137-a16b-a91345d8a931%252FUntitled.png%3Ftable%3Dblock%26id%3D67aeb6d6-f1e1-44b5-8790-d48ab129149e%26cache%3Dv2&w=3840&q=75)
목적을 선택 후 활용 신청을 한다.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252Ff5dec925-e7c5-46ea-b550-65d610b3e603%252FUntitled.png%3Ftable%3Dblock%26id%3D9d803b90-506e-43b9-b37c-fa9960c5b227%26cache%3Dv2&w=3840&q=75)
마이 페이지 - 데이터 활용 - Open API - 활용 신청 현황을 들어간다.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F773fea9a-b357-49ed-8cef-6754432921c2%252FUntitled.png%3Ftable%3Dblock%26id%3D1327eb51-c5a5-4036-a86e-c21dea99ef87%26cache%3Dv2&w=3840&q=75)
End Point : 클라이언트가 요청을 보내는 URL
일반 인증키(Encoding) : URL에 포함될 수 있도록 특수 문자를 인코딩한 형태. 인코딩된 키는 URL의 쿼리 파라미터로 사용하기 적합하다
일반 인증키(Decoding) : URL 인코딩이 되지 않은 순수한 문자열이다.
== 같은 문자는 키워드 값으로 URL에 사용할 수 없다. 주소를 파싱하는데 기준이 되는 키워드이기 때문이다. 따라서 인코딩된 인증키를 URL에 사용해야 한다. 다만 브라우저나 특정 라이브러리는 자동으로 변환해주기 때문에 디코딩 인증키를 사용해보고 안되면 인코딩 인증키를 사용하자.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F86fae572-ea2b-41d6-9ec6-083527f9a3f9%252FUntitled.png%3Ftable%3Dblock%26id%3Deefa0ea0-d3ef-463f-baf4-25065a0c7e32%26cache%3Dv2&w=828&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F86fae572-ea2b-41d6-9ec6-083527f9a3f9%252FUntitled.png%3Ftable%3Dblock%26id%3Deefa0ea0-d3ef-463f-baf4-25065a0c7e32%26cache%3Dv2&w=828&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252Fce4a2226-5857-4261-b977-3fdec0a2d5d7%252FUntitled.png%3Ftable%3Dblock%26id%3D8460f680-d428-4a3f-8fb4-69f42cec0cf5%26cache%3Dv2&w=2048&q=75)
아래에 활용신청 상세기능정보에서 값을 넣어 테스트를 해볼 수 있다. dataType을 json으로 넣고 테스트한다.
https://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtNcst?serviceKey=GwP4SAIbheNGehkwEohRbKiZarjkYCbbT0%2FKzCWnPd7XL0vo97Tvx8b1Q4wwORGuGx47qa34CKrCDe4AL4vCrw%3D%3D&pageNo=1&numOfRows=1000&dataType=json&base_date=20240607&base_time=1900&nx=55&ny=127
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F2000799e-a2f8-4532-b0bf-97ffe74880d1%252FUntitled.png%3Ftable%3Dblock%26id%3D9fc20d74-c008-427e-b408-8961395b1e6e%26cache%3Dv2&w=3840&q=75)
미리보기를 누르면 데이터가 조회된다.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F9c021838-fe00-49c4-a8f2-873b5414340e%252FUntitled.png%3Ftable%3Dblock%26id%3Dd2407e4d-2526-4979-8963-46cbaaca3931%26cache%3Dv2&w=2048&q=75)
어떤 값이 온도인지는 참고문서를 확인하면 알 수 있다. T1H가 온도이다.
2. Java 샘플 코드 테스트
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252Fb35ba2aa-ce62-4988-a5a5-307052b4d691%252FUntitled.png%3Ftable%3Dblock%26id%3D1b601beb-5946-403d-8d59-29e44a0ed7b0%26cache%3Dv2&w=3840&q=75)
/* Java 1.8 샘플 코드 */ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.io.BufferedReader; import java.io.IOException; public class ApiExplorer { public static void main(String[] args) throws IOException { StringBuilder urlBuilder = new StringBuilder("http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtNcst"); /*URL*/ urlBuilder.append("?" + URLEncoder.encode("serviceKey","UTF-8") + "=서비스키"); /*Service Key*/ urlBuilder.append("&" + URLEncoder.encode("pageNo","UTF-8") + "=" + URLEncoder.encode("1", "UTF-8")); /*페이지번호*/ urlBuilder.append("&" + URLEncoder.encode("numOfRows","UTF-8") + "=" + URLEncoder.encode("1000", "UTF-8")); /*한 페이지 결과 수*/ urlBuilder.append("&" + URLEncoder.encode("dataType","UTF-8") + "=" + URLEncoder.encode("XML", "UTF-8")); /*요청자료형식(XML/JSON) Default: XML*/ urlBuilder.append("&" + URLEncoder.encode("base_date","UTF-8") + "=" + URLEncoder.encode("20210628", "UTF-8")); /*‘21년 6월 28일 발표*/ urlBuilder.append("&" + URLEncoder.encode("base_time","UTF-8") + "=" + URLEncoder.encode("0600", "UTF-8")); /*06시 발표(정시단위) */ urlBuilder.append("&" + URLEncoder.encode("nx","UTF-8") + "=" + URLEncoder.encode("55", "UTF-8")); /*예보지점의 X 좌표값*/ urlBuilder.append("&" + URLEncoder.encode("ny","UTF-8") + "=" + URLEncoder.encode("127", "UTF-8")); /*예보지점의 Y 좌표값*/ 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(); System.out.println(sb.toString()); } }
오픈API 상세 페이지 하단에서 샘플코드를 확인할 수 있다.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252F31ea94cc-7535-46ce-bc9f-a2cc045319e1%252FUntitled.png%3Ftable%3Dblock%26id%3D157b0bd4-5ccf-4077-90a4-9286df376705%26cache%3Dv2&w=2048&q=75)
새로운 자바 프로젝트를 만든다.
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252F404a9fb6-ab9b-45cc-8074-ee63a3334890%252Fb422eafa-f060-4ec5-b514-d54cb3cf9064%252FUntitled.png%3Ftable%3Dblock%26id%3D7db22ef3-1bcf-4e6f-9525-f8b22bfc55ad%26cache%3Dv2&w=1920&q=75)
샘플 코드를 넣고 실행하면 이런 출력을 볼 수 있다.
Share article