์ธ์ฆ๋ฐ์ ํค๋ฅผ ๋ฃ๊ณ url์ ๋ง๋ค์ด์ ์ ๋ณด๋ฅผ json์ผ๋ก ๋ฐ์์ค๊ณ ํ์ฑํฉ๋๋ค.
ํ์ฑํ ๋ jar ํ์ผ์ ํ๋ ์ถ๊ฐํด์ผ ํด์!!!
์์ถํด๋๋ก ์ฌ๋ ค๋๋ฆฝ๋๋น ๋ค์ด๋ฐ์ผ์๊ณ ์์ถ ํด์ ํด์ฃผ์ธ์ฉ
ํ๋ก์ ํธ ์ฐํด๋ฆญ > bulid path > Configuration build path ๋ค์ด๊ฐ์ฃผ์ธ์.
Libraries > modulepath> add external jars ํด์ jar ํ์ผ์ ์ถ๊ฐํด์ค๋๋น.
------> url์์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ชป๋ฐ์์ค๋!!! ์ ์๋ถ๋ฌ์ง๋!!!! ๊ทธ๋ด๋๋??
๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค์ง ๋ชปํ ๋๋ ์ฝ๋๋ก ์ง url์ ์ฝ์์ ์ฐ์ด๋ณด์๊ณ ,
๊ธฐ์์ฒญ์์ ๋ฏธ๋ฆฌ๋ณด๊ธฐํ ํ์ด์ง์ url๊ณผ ๋.๊ฐ.๋ ๋น๊ตํด๋ด ๋๋ค.
๋,์๋ฌธ์, ๋ฌผ์ํ, ์์ ์ด๋ค๊ฑฐ ํ๋ ๋น ์ง์์ด ๋.๊ฐ.๋ค.๋ฉด ๋ค~ ๋ถ๋ฌ์์ ธ์ฉ!!
ํ๋ด์ธ์!!
์๋ ํ๋ฉด์ url์ ๋๋ค.
--------> ๋ ์ง๋ ๋ง์๋๋ก (์ ๋ ๋ถํฐ ๋ชจ๋๊น์ง๋ง) ์ ๋ ฅํด์ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํด ๋ณผ ์ ์๋๋ฐ์,
์ ๋ ๊ท์ฐฎ์์ ๋น์ผ ๋ ์ง ์๋ฐ์์ date๋ก ๋ฐ์์ format ๊ณต๊ณต๋ฐ์ดํฐํฌํธ ํ์์ ๋ง์ถฐ์ ๊ฐ์ ธ์ค๊ฒ ํ์ด์.
----------> ์ ๋ ๋ฐ์์ค๋ ํญ๋ชฉ์ ๊ธฐ์จ, ์ต๋, ๋ ์ง, ์๊ฐ ์ด๋ ๊ฒ 4๊ฐ๋ง ๋ฐ์์๋๋ฐ์
์ํ์๋ ํญ๋ชฉ์ ๊ณจ๋ผ์ ์ค์์น๋ฌธ ์์ ์ผ์ด์ค์ ์ ๋ ๊ฒ ๋ฃ์ด์ฃผ์๋ฉด ๋ฉ๋๋น~~~~~
package com.weather03.mvc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class VillageWeatherJson {
//List<VillageWeather> datalist = new ArrayList<VillageWeather>();
public List<VillageWeather> getVillageWeather() throws IOException, ParseException {
DateFormat sdFormat = new SimpleDateFormat("yyyyMMdd");
Date nowDate = new Date();
String tempDate = sdFormat.format(nowDate);
// JSON๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๋ URLstr์ ๋ง๋ญ๋๋ค.
String apiUrl = "http://apis.data.go.kr/1360000/VilageFcstInfoService/getVilageFcst";
// ํํ์ด์ง์์ ๋ฐ์ ํค
String serviceKey = "์ธ์ฆ๋ฐ์ ํค ๋ณต๋ถ ํด ์ฃผ์ธ์!";
String pageNo = "1";
String numOfRows = "225"; // ํ ํ์ด์ง ๊ฒฐ๊ณผ ์
String data_type = "JSON"; // ํ์
xml, json ๋ฑ๋ฑ ..
String baseDate = tempDate; // "20200821"์ด๋ฐ์์ผ๋ก api์์ ์ ๊ณตํ๋ ํ์ ๊ทธ๋๋ก ์ ์ผ์๋ฉด ๋ฉ๋๋น.
String baseTime = "0500"; // API ์ ๊ณต ์๊ฐ์ ์
๋ ฅํ๋ฉด ๋จ
String nx = "60"; // ์๋
String ny = "120"; // ๊ฒฝ๋
// ์ ๋ 23์ ๋ถํฐ 153๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ฉด ์ค๋๊ณผ ๋ด์ผ์ ๋ ์จ๋ฅผ ์ ์ ์์
VillageWeather vl = new VillageWeather();
StringBuilder urlBuilder = new StringBuilder(apiUrl);
urlBuilder.append("?" + URLEncoder.encode("serviceKey", "UTF-8") + "=" + serviceKey);
urlBuilder.append("&" + URLEncoder.encode("pageNo", "UTF-8") + "=" + URLEncoder.encode(pageNo, "UTF-8"));
urlBuilder.append("&" + URLEncoder.encode("numOfRows", "UTF-8") + "="+ URLEncoder.encode(numOfRows, "UTF-8")); /* ํ ํ์ด์ง ๊ฒฐ๊ณผ ์ */
urlBuilder.append("&" + URLEncoder.encode("dataType", "UTF-8") + "=" + URLEncoder.encode(data_type, "UTF-8")); /* ํ์
*/
urlBuilder.append("&" + URLEncoder.encode("base_date", "UTF-8") + "="+ URLEncoder.encode(baseDate, "UTF-8")); /* ์กฐํํ๊ณ ์ถ์ ๋ ์ง */
urlBuilder.append("&" + URLEncoder.encode("base_time", "UTF-8") + "="+ URLEncoder.encode(baseTime, "UTF-8")); /* ์กฐํํ๊ณ ์ถ์ ์๊ฐ AM 02์๋ถํฐ 3์๊ฐ ๋จ์ */
urlBuilder.append("&" + URLEncoder.encode("nx", "UTF-8") + "=" + URLEncoder.encode(nx, "UTF-8")); // ๊ฒฝ๋
urlBuilder.append("&" + URLEncoder.encode("ny", "UTF-8") + "=" + URLEncoder.encode(ny, "UTF-8")+"&"); // ์๋
/* GET๋ฐฉ์์ผ๋ก ์ ์กํด์ ํ๋ผ๋ฏธํฐ ๋ฐ์์ค๊ธฐ*/
URL url = new URL(urlBuilder.toString());
// ์ด๋ป๊ฒ ๋์ด๊ฐ๋์ง ํ์ธํ๊ณ ์ถ์ผ๋ฉด ์๋ ์ถ๋ ฅ๋ถ ์ฃผ์ ํด์
System.out.println(url);
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();
System.out.println("๊ฒฐ๊ณผ: " + result);
// ๋ฌธ์์ด์ JSON์ผ๋ก ํ์ฑํฉ๋๋ค. ๋ง์ง๋ง ๋ฐฐ์ดํํ๋ก ์ ์ฅ๋ ๋ฐ์ดํฐ๊น์ง ํ์ฑํด๋
๋๋ค.
JSONParser jsonParser = new JSONParser();
JSONObject jsonObj = (JSONObject) jsonParser.parse(result);
JSONObject parse_response = (JSONObject) jsonObj.get("response");
JSONObject parse_body = (JSONObject) parse_response.get("body");// response ๋ก ๋ถํฐ body ์ฐพ์์ค๊ธฐ
JSONObject parse_items = (JSONObject) parse_body.get("items");// body ๋ก ๋ถํฐ items ๋ฐ์์ค๊ธฐ
// items๋ก ๋ถํฐ itemlist ๋ฅผ ๋ฐ์์ค๊ธฐ itemlist : ๋ค์ [ ๋ก ์์ํ๋ฏ๋ก jsonarray์ด๋ค.
JSONArray parse_item = (JSONArray) parse_items.get("item");
JSONObject obj;
String category; // ๊ธฐ์ค ๋ ์ง์ ๊ธฐ์ค์๊ฐ์ VillageWeather ๊ฐ์ฒด์ ์ ์ฅํฉ๋๋ค.
String day = "";
String time = "";
List<VillageWeather> datalist = new ArrayList<VillageWeather>();
for (int i = 0; i < parse_item.size(); i++) {
obj = (JSONObject) parse_item.get(i); // ํด๋น item์ ๊ฐ์ ธ์ต๋๋ค.
Object fcstValue = obj.get("fcstValue");
Object fcstDate = obj.get("fcstDate");
Object fcstTime = obj.get("fcstTime");
category = (String) obj.get("category"); // item์์ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ๊ฒ์ํด์ต๋๋ค.
// ๊ฒ์ํ ์นดํ
๊ณ ๋ฆฌ์ ์ผ์นํ๋ ๋ณ์์ ๋ฌธ์ํ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํฉ๋๋ค.
// ๋ฐ์ดํฐ๋ค์ด ํํ๊ฐ ๋ฌ๋ผ ๋ฌธ์์ด๋ก ํต์ผํด์ผ ํธํฉ๋๋ค. ๊บผ๋ด์ ์ฌ์ฉํ ๋ ๋ค์๋ณํํ๋๊ฒ ์ข์ต๋๋ค.
switch (category) {
case "REH":
vl.reh = (obj.get("fcstValue")).toString();
break;
case "T3H":
vl.t3h = (obj.get("fcstValue")).toString();
break;
}
if (!day.equals(fcstDate.toString())) {
day = fcstDate.toString();
}
if (!time.equals(fcstTime.toString())) {
time = fcstTime.toString();
System.out.println("๋ฐ์ด: "+ day + " " +"ํ์: "+ time);
}
vl.baseDate = (obj.get("fcstDate")).toString();
vl.baseTime = (obj.get("fcstTime")).toString();
VillageWeather test = new VillageWeather();
test.setT3h(vl.getT3h());
test.setReh(vl.getReh());
test.setBaseDate(vl.getBaseDate());
test.setBaseTime(vl.getBaseTime());
System.out.print("\tcategory : " + category);
System.out.print(", fcst_Value : " + fcstValue);
System.out.print(", fcstDate : " + vl.baseDate);
System.out.println(", fcstTime : " + vl.baseTime);
//test = vl;
if(category.equals("T3H")) {
System.out.println("HAHA!");
datalist.add(test);
}else {
System.out.println("ใ
ใ
");
}
}
return datalist;// ๋ชจ๋ ๊ฐ์ด ์ ์ฅ๋ VillageWeather๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
}
}
//ํ๋ก์ ํธ ํต์ผ๋ก ์ฌ๋ ค๋ ๊นํ๋ธ ์ฃผ์์ ๋๋ค.
๋ค๋ฅธ ๋ด์ฉ์ด ๊ถ๊ธํ์๋ค๋ฉด ํ์ธ ํด ์ฃผ์ธ์.
github.com/Joowon0220/weather.git
๋๊ธ