在做网站中,遇到天气预报接口是避免不了的,天气预报的实现主要涉及到以下几个技术点:
附效果地址:http://weather.faqee.com/
定义缓存:
private static HashMap<String,JSONObject> hmCache = new HashMap();
缓存通过TimerTask定时清空前几天实现,代码如下:
public static void cleanCache() { if(isStart) return; isStart = true; TimerTask task = new TimerTask() { public void run() { Iterator it = hmCache.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); String today = DateTimeUtil.format(new Date(),"yyyyMMdd"); if(key.toString().indexOf(today)>=0){ it.remove(); hmCache.remove(key); } } } }; Timer timer = new Timer(); timer.schedule(task, Calendar.getInstance ().getTime(), 24*3600 * 1000); }
拉取天气:
public JSONObject getWeather(String city) { String today = DateTimeUtil.format(new Date(),"yyyyMMdd"); if(hmCache.get(city+today)!=null){ return hmCache.get(city+today); } JSONObject hm =new JSONObject(); hm.put("zhishu",""); try { city = getCityName(city); Document doc; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream is = getSoapInputStream(city); if(is == null) return hm; doc = db.parse(is); NodeList nl = doc.getElementsByTagName("Date"); String tpPrefix = "http://www.nmc.gov.cn/img/weather/dn_icon/"; String t = "",t_res = "",t_tp=""; String t1 = "",t1_res = "",t1_tp=""; String t2 = "",t2_res = "",t2_tp=""; String t3 = "",t3_res = "",t3_tp=""; for (int count = 0; count < nl.getLength(); count++) { Node n = nl.item(count); NodeList n2 = n.getChildNodes(); t = doc.getElementsByTagName("Sj0").item(0).getFirstChild().getNodeValue(); t1 = doc.getElementsByTagName("Sj1").item(0).getFirstChild().getNodeValue(); t2 = doc.getElementsByTagName("Sj2").item(0).getFirstChild().getNodeValue(); t3 = doc.getElementsByTagName("Sj3").item(0).getFirstChild().getNodeValue(); if(n2!=null){ for(int jj=0;jj<n2.getLength();jj++){ Node nn = n2.item(jj); if(count == 0 ) // 今天 { if(nn.getNodeName().equalsIgnoreCase("tq")){ t_res += nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Qw")){ t_res += ","+nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Fx")){ t_res += ","+nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Rc")){ t_res += ","+nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Rl")){ t_res += ","+nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("TP")){ t_tp = nn.getFirstChild().getNodeValue(); t_tp = Utils.replace(t_tp,"\"",""); t_tp = Utils.replace(t_tp,"/images/",""); t_tp = tpPrefix + t_tp; } }else if(count == 1) //明天 { if(nn.getNodeName().equalsIgnoreCase("tq")){ t1_res += nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Qw")){ t1_res += ","+nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Fx")){ t1_res += ","+nn.getFirstChild().getNodeValue(); } else if(nn.getNodeName().equalsIgnoreCase("TP")){ t1_tp = nn.getFirstChild().getNodeValue(); t1_tp = Utils.replace(t1_tp,"\"",""); t1_tp = Utils.replace(t1_tp,"/images/",""); t1_tp = tpPrefix + t1_tp; } }else if(count == 2) //后天 { if(nn.getNodeName().equalsIgnoreCase("tq")){ t2_res += nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Qw")){ t2_res += ","+nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Fx")){ t2_res += ","+nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("TP")){ t2_tp = nn.getFirstChild().getNodeValue(); t2_tp = Utils.replace(t2_tp,"\"",""); t2_tp = Utils.replace(t2_tp,"/images/",""); t2_tp = tpPrefix + t2_tp; } }else if(count == 3) //大后天 { if(nn.getNodeName().equalsIgnoreCase("tq")){ t3_res += nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Qw")){ t3_res += ","+nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("Fx")){ t3_res += ","+nn.getFirstChild().getNodeValue(); }else if(nn.getNodeName().equalsIgnoreCase("TP")){ t3_tp = nn.getFirstChild().getNodeValue(); t3_tp = Utils.replace(t3_tp,"\"",""); t3_tp = Utils.replace(t3_tp,"/images/",""); t3_tp = tpPrefix + t3_tp; } } } } } is.close(); hm.put("t", t); hm.put("t_result", t_res); hm.put("t_tp", t_tp.trim()); hm.put("t1", t1); hm.put("t1_result", t1_res); hm.put("t1_tp", t1_tp.trim()); hm.put("t2", t2); hm.put("t2_result", t2_res); hm.put("t2_tp", t2_tp.trim()); hm.put("t3", t3); hm.put("t3_result", t3_res); hm.put("t3_tp", t3_tp.trim()); hmCache.put(city+today, hm); return hm; } catch (Exception e) { e.printStackTrace(); return null; } }
附效果地址:http://weather.faqee.com/