天气预报代码分析与实现
2009-09-17 00:00:00 来源:WEB开发网拉取天气:
Java代码
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/
更多精彩
赞助商链接