?? weather.jsp
字號:
<%@ page contentType="text/plain; charset=UTF-8"%>
<%@ page language="java"%>
<%@ page import="java.util.*,java.io.*,org.dom4j.*,org.dom4j.io.*"%>
<%!
//根據風向角度轉換為風向信息
String getDirection(String degreeStr) {
int degree = Integer.parseInt(degreeStr);
String direction = null;
if(degree > 337 && degree <= 360) {
direction = "北風";
} else if(degree >= 0 && degree <= 22) {
direction = "北風";
} else if(degree > 22 && degree <= 67) {
direction = "東北風";
} else if(degree > 67 && degree <= 112) {
direction = "東風";
} else if(degree > 112 && degree <= 157) {
direction = "東南風";
} else if(degree > 157 && degree <= 202) {
direction = "南風";
} else if(degree > 202 && degree <= 247) {
direction = "西南風";
} else if(degree > 247 && degree <= 292) {
direction = "西風";
} else if(degree > 292 && degree <= 337) {
direction = "西北風";
}
return direction;
}
%>
<%
out.clear(); //清空當前的輸出內容(空格和換行符)
String cityCode = request.getParameter("cityCode"); //獲取城市代碼
SAXReader reader = new SAXReader(); //創建一個SAXReader
//使用Reader解析遠程服務器RSS信息
Document doc = reader.read("http://xml.weather.yahoo.com/forecastrss?p="+cityCode+"&u=c");
//將需要進一步處理的信息轉換為Node對象
Node lastBuildDateNode = doc.selectSingleNode("//rss/channel/lastBuildDate");
Node windNode = doc.selectSingleNode("//rss/channel/yweather:wind");
Node atmosphereNode = doc.selectSingleNode("//rss/channel/yweather:atmosphere");
Node astronomyNode = doc.selectSingleNode("//rss/channel/yweather:astronomy");
Node conditionNode = doc.selectSingleNode("//rss/channel/item/yweather:condition");
Node forecastNode = doc.selectSingleNode("//rss/channel/item/yweather:forecast");
//獲取需要的信息
String lastBuildDate = lastBuildDateNode.getText(); //最后更新日期
String chill = "N/A";
String direction = "N/A";
String speed = "N/A";
if (windNode != null) {
chill = windNode.valueOf("@chill") + "℃"; //感覺氣溫
direction = getDirection(windNode.valueOf("@direction")); //風向
speed = windNode.valueOf("@speed") + "Kph"; //風速
}
String humidity = "N/A";
String visibility = "N/A";
if (atmosphereNode != null) {
humidity = atmosphereNode.valueOf("@humidity") + "%"; //濕度
visibility = Double.parseDouble(atmosphereNode.valueOf("@visibility"))/100 + "km";//能見度
}
String sunrise = astronomyNode.valueOf("@sunrise"); //日出時間
String sunset = astronomyNode.valueOf("@sunset"); //日落時間
String condition = "N/A";
String temp = "N/A";
if (conditionNode != null) {
condition = conditionNode.valueOf("@text"); //天氣狀況
temp = conditionNode.valueOf("@temp") + "℃"; //實際氣溫
}
String low = forecastNode.valueOf("@low") + "℃"; //最低氣溫
String high = forecastNode.valueOf("@high") + "℃"; //最高氣溫
//將信息拼接為JSON格式
StringBuffer result = new StringBuffer("{");
result.append("'lastBuildDate':'" + lastBuildDate + "',");
result.append("'chill':'" + chill + "',");
result.append("'direction':'" + direction + "',");
result.append("'speed':'" + speed + "',");
result.append("'humidity':'" + humidity + "',");
result.append("'visibility':'" + visibility + "',");
result.append("'sunrise':'" + sunrise + "',");
result.append("'sunset':'" + sunset + "',");
result.append("'condition':'" + condition + "',");
result.append("'temp':'" + temp + "',");
result.append("'low':'" + low + "',");
result.append("'high':'" + high + "'");
result.append("}");
out.print(result.toString()); //輸出天氣信息
%>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -