반응형
기상청 API 이용하기 위해 진행한 소스코드..
몇몇 분들의 블로그를 참조하기도 했습니다.. (꾸벅 감사드립니다)
이 소스는 실력이 부족한 제가 했기 때문에.. 많이 지저분하고 비 효율적인 소스 입니다 ㅠ
좀 더 효율적이고 더 쉽게 코드를 짜신 분은 알려주세요!
궁금하신 점은 댓글로 말씀해주세요 감사합니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
import java.io.BufferedReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
class Weatherdatb{
/***********************************************************************
* 현재시간 가져오기
*
* @return
* *********************************************************************/
public String fn_time()
{
SimpleDateFormat Format = new SimpleDateFormat("yyyyMMDD HHmmss");
Date time = new Date();
String timedata = Format.format(time);
return timedata;
}
/********************************************************************************
* 필요데이터 : 경도 및 위도, 현재날짜 및 시간, 발급받은 서비스키
* 위도 경도에 따른 지역 날씨 예보 정보 가져옴
* 3시간 단위로 날씨 데이터 업데이트(기상청)
* @param baseDate
* @param baseTime
* @param nx
* @param ny
* @return
* *******************************************************************************/
public String fn_APIConnect(String baseDate, String baseTime, String nx, String ny)
{
String ConnectValue = "";
BufferedReader br;
String servicekey = "서비스 키 ";
String address = "http://newsky2.kma.go.kr/service/SecndSrtpdFrcstInfoService2/ForecastSpaceData?serviceKey="
+ servicekey + "&base_date=" + baseDate + "&base_time=" + baseTime + "&nx=" + nx + "&ny=" + ny + "&_type=json";
try
{
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-type", "application/json");
if(conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300)
{
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
}
else
{
br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
String ResData = br.readLine();
if(ResData == null)
{
System.out.println("응답데이터 == NULL");
}
else
{
ConnectValue = fn_Jsonp(ResData);
}
conn.disconnect();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
return ConnectValue;
}
/****************************************************************************
* JSON 데이터 파싱함수
* 데이터 추출
* @param Data
* **************************************************************************/
public String fn_Jsonp(String Data)
{
JSONObject WeatherData;
String VALUE = "";
String date = "";
String time = "";
String DataValue = "";
String info = "";
try
{
JSONParser parsar = new JSONParser();
JSONObject obj = (JSONObject) parsar.parse(Data);
{
WeatherData = (JSONObject) item.get(i);
if(info.equals("POP")) {
info = "강수확률";
DataValue = DataValue+" %";
}
if(info.equals("REH")) {
info = "습도";
DataValue = DataValue+" %";
}
if(info.equals("SKY")) {
info = "하늘상태";
if(DataValue.equals("1")) {
DataValue = "맑음";
}else if(DataValue.equals("2")) {
DataValue = "비";
}else if(DataValue.equals("3")) {
DataValue = "구름많음";
}else if(DataValue.equals("4")) {
DataValue = "흐림";
}
}
if(info.equals("UUU")) {
info = "동서성분풍속";
DataValue = DataValue+" m/s";
}
if(info.equals("VVV")) {
info = "남북성분풍속";
DataValue = DataValue+" m/s";
}
if(info.equals("T1H")) {
info = "기온";
DataValue = DataValue+"℃";
}
if(info.equals("R06")) {
info = "6시간강수량";
DataValue = DataValue + " mm";
}
if(info.equals("S06")) {
info = "6시간적설량";
DataValue = DataValue + " mm";
}
if(info.equals("PTY")){
info = "강수형태";
if(DataValue.equals("0")) {
DataValue = "없음";
}else if(DataValue.equals("1")) {
DataValue = "비";
}else if(DataValue.equals("2")) {
DataValue = "눈/비";
}else if(DataValue.equals("3")) {
DataValue = "눈";
}
}
if(info.equals("T3H")) {
info = "3시간기온";
DataValue = DataValue + " ℃";
}
if(info.equals("VEC")) {
info = "풍향";
DataValue = DataValue + " m/s";
}
VALUE += info + "," + DataValue + "," + date + "," + time + ",";
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}
return VALUE;
}
/**********************************************************************
* 현재시간을 가져와서 ex) 1000 형태로 만들어줌
* 3시간 마다 업데이트 되기 때문에 각 시간에 따라 업데이트 시간으로 설정
* @param timedata
* @return
* *********************************************************************/
public String fn_timeChange(String timedata)
{
String hh = timedata.substring(9, 11);
String baseTime = "";
hh = hh + "00";
// 현재 시간에 따라 데이터 시간 설정(3시간 마다 업데이트) //
switch(hh) {
case "0200":
case "0300":
case "0400":
baseTime = "0200";
break;
case "0500":
case "0600":
case "0700":
baseTime = "0500";
break;
case "0800":
case "0900":
case "1000":
baseTime = "0800";
break;
case "1100":
case "1200":
case "1300":
baseTime = "1100";
break;
case "1400":
case "1500":
case "1600":
baseTime = "1400";
break;
case "1700":
case "1800":
case "1900":
baseTime = "1700";
break;
case "2000":
case "2100":
case "2200":
baseTime = "2000";
break;
default:
baseTime = "2300";
}
return baseTime;
}
}
public class weatherb {
public static void main(String[] args) {
String Response = "";
String[] ResponseData ;
String end = "";
Weatherdatb WeatherAPI = new Weatherdatb();
//fn_time 함수를 사용하여 현재시간 받아오기
String timedata = WeatherAPI.fn_time();
//현재시간을 활용하여 기상청 시간데이터 형식에 맞게 변환
String HH = WeatherAPI.fn_timeChange(timedata);
String YMD = timedata.substring(0, 8);
//경도 //위도
String nx = "60";
String ny = "127";
//연결하기 위한 파라미터들
Response = WeatherAPI.fn_APIConnect(YMD, HH, nx, ny);
ResponseData = Response.split(",");
System.out.print(ResponseData[0]);
System.out.println(" " + ResponseData[1]);
System.out.print(ResponseData[2]);
System.out.println(" " + ResponseData[3]);
System.out.print(ResponseData[4]);
System.out.println(" " + ResponseData[5]);
System.out.print(ResponseData[8]);
System.out.println(" " + ResponseData[9]);
System.out.print(ResponseData[12]);
System.out.println(" " + ResponseData[13]);
System.out.print(ResponseData[16]);
System.out.println(" " + ResponseData[17]);
System.out.print(ResponseData[20]);
System.out.println(" " + ResponseData[21]);
System.out.print(ResponseData[24]);
System.out.println(" " + ResponseData[25]);
System.out.print(ResponseData[28]);
System.out.println(" " + ResponseData[29]);
System.out.print(ResponseData[32]);
System.out.println(" " + ResponseData[33]);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
반응형
'JAVA' 카테고리의 다른 글
기상청 데이터 PostgresqlDB 연동하기)(DTO,DAO) (0) | 2019.06.18 |
---|---|
[기상청 API] 종관기상관측 사용하기 (0) | 2019.06.14 |
Model1 간략한 로그인 기능 만들기 - 회원정보보기(list) (0) | 2019.05.28 |
Model1 간략한 로그인 기능 만들기 - 회원정보수정(update) (0) | 2019.05.28 |
Model1 간략한 로그인 기능 만들기 - 로그인 (0) | 2019.05.28 |