Programming
No. | 479 | ||||
Name. | swindler | ||||
Subject. | 구글 날씨 API 적용 | ||||
Main Cate. | Java | ||||
Sub Cate. | |||||
Date. | 2008-10-14 19:23 | ||||
Hit. | 7743 (210.182.190.136) | ||||
File. | |||||
구글의 날씨 API는 아래 주소에서 받아올수 있다. http://www.google.co.kr/ig/api?weather=seoul 네이놈처럼 치사하게 인증키 받고 하루에 조회건수 제외하고 이런게 없어서 참 좋다. xml은 워낙 심플해서 더이상 설명이 필요치 않다. <?xml version="1.0" ?> - <xml_api_reply version="1"> - <weather module_id="0" tab_id="0"> - <forecast_information> <city data="seoul" /> <postal_code data="seoul" /> <latitude_e6 data="" /> <longitude_e6 data="" /> <forecast_date data="2008-10-14" /> <current_date_time data="2008-10-14 10:00:00 +0000" /> <unit_system data="SI" /> </forecast_information> - <current_conditions> <condition data="대체로 흐림" /> <temp_f data="59" /> <temp_c data="15" /> <humidity data="습도: 77%" /> <icon data="/images/weather/mostly_cloudy.gif" /> <wind_condition data="바람: 서풍, 3 km/h" /> </current_conditions> - <forecast_conditions> <day_of_week data="오늘" /> <low data="12" /> <high data="22" /> <icon data="/images/weather/sunny.gif" /> <condition data="맑음" /> </forecast_conditions> - <forecast_conditions> <day_of_week data="수" /> <low data="13" /> <high data="25" /> <icon data="/images/weather/sunny.gif" /> <condition data="맑음" /> </forecast_conditions> - <forecast_conditions> <day_of_week data="목" /> <low data="14" /> <high data="25" /> <icon data="/images/weather/mostly_sunny.gif" /> <condition data="대체로 맑음" /> </forecast_conditions> - <forecast_conditions> <day_of_week data="금" /> <low data="14" /> <high data="26" /> <icon data="/images/weather/sunny.gif" /> <condition data="맑음" /> </forecast_conditions> </weather> </xml_api_reply> 그냥 구현할려면 시간이 좀 걸릴수도 있으나, 몇가지 만들어둔 모듈을 활용하면 대략 아래와 같다. String url = "http://www.google.co.kr/ig/api?weather=seoul"; FetchURL f = new FetchURL(); String xml = f.getContent(url); ByteArrayInputStream bs = new ByteArrayInputStream(xml.getBytes("utf-8")); InputSource is = new InputSource(bs); DomXML dxml = new DomXML(is,null); day1 = JspUtil.null2void(dxml.getAttrValue("xml_api_reply/weather/forecast_conditions/day_of_week", 1, "data")); FetchURL은 url 주소를 주면 내용을 반환해주는 클래스이고, DomXML은 xml 문서를 dom 으로 분석해서 원하는 데이터를 쉽게 추출할 수 있도록 만든 클래스이다. 한가지 문제가 페이지 부를때마다 google 주소를 호출하는 것은 비효율적이므로 이 부분에 대한 캐싱이 필요하다. (홈페이지에 적용하기 위해서..) 그 부분에 대한 소스는 skip 하도록 하겠다. 처음에는 간략하게 cookie를 사용하도록 만들었으나, 이건 사용자별로는 계속해서 호출을 하게 되므로, 정적으로 선언하여 주기적으로 받아오게 수정했다. [바로가기 링크] : http://coolx.net/cboard/develop/479 |
|||||
|
|||||
[Modify] [Delete] | [Reply] [List] |