l ๐Ÿค– ์ฝ”๋“œ l

๊ธฐ์ƒ์ฒญ ๋‚ ์”จapi JSON์œผ๋กœ ๋ฐ›์•„์™€์„œ ํŒŒ์‹ฑํ•˜๊ธฐ (์•ˆ๋ถˆ๋Ÿฌ์™€์งˆ๋•Œ!! ์—๋Ÿฌํ•ด๊ฒฐ)

์ฝ”๋“œ๊ทธ๋ฆผ 2020. 5. 21.
๋ฐ˜์‘ํ˜•

์ธ์ฆ๋ฐ›์€ ํ‚ค๋ฅผ ๋„ฃ๊ณ  url์„ ๋งŒ๋“ค์–ด์„œ ์ •๋ณด๋ฅผ json์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ  ํŒŒ์‹ฑํ•ฉ๋‹ˆ๋‹ค.

 

 

 

ํŒŒ์‹ฑํ• ๋•Œ jar ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ด์š”!!!

์••์ถ•ํด๋”๋กœ ์˜ฌ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹น ๋‹ค์šด๋ฐ›์œผ์‹œ๊ณ  ์••์ถ• ํ•ด์ œํ•ด์ฃผ์„ธ์šฉ

json-simple-1.1.1.jar.zip
0.02MB

 

 

ํ”„๋กœ์ ํŠธ ์šฐํด๋ฆญ > 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

 

GitHub - Joowon0220/weather: weatherAPI

weatherAPI. Contribute to Joowon0220/weather development by creating an account on GitHub.

github.com

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€

๐Ÿ’ฒ ์ถ”์ฒœ ๊ธ€