標籤:
運行效果:
主要功能:
1,jsp頁面輸入省份和城市 根據條件擷取當地的天氣資訊
2,java代碼 利用第三方的省份和城市的路徑地址
本工程主要實現java擷取天氣預報的資訊
步驟
1,建立工程weatherDemo
2,建立包結構
3,建立類
4,訪問第三方介面 開啟主機方法
5,擷取省份id方法
6,擷取市id方法
7,擷取天氣的方法
8,編寫servlet
9,發布運行
java代碼
建立WeatherDemo類
/**
* @version 1.0
* @author ren
* 天氣預報的核心介面
* */
public class WeatherDemo {
private static String SERVIES_HOST = "www.webxml.com.cn"; //主機地址 第三方的
private static String WEATHER_SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
//省份的id
private static String PROVINCE_CODE_URL = WEATHER_SERVICE_URL + "getRegionProvince";
//城市的id
private static String CITY_CODE_URL = WEATHER_SERVICE_URL + "getSupportCityString?theRegionCode=";
//天氣的地址
private static String WEATHER_QUERY_URL = WEATHER_SERVICE_URL + "getWeather?theUserid=&theCityCode=";
/**
* 開啟伺服器主機的方法
* */
public static InputStream getSoapInputStream(String url){
InputStream inputStream = null;
try {
URL urlObj = new URL(url);
URLConnection urlConn =urlObj.openConnection(); //開啟串連
urlConn.setRequestProperty("Host", SERVIES_HOST);//訪問主機
urlConn.connect();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return inputStream;
}
/**
* 擷取省份的編碼
*
* */
public static int getProinceCode(String proinceName){
org.w3c.dom.Document document ;
int proinceId = 0; //省份id
//解析xml
//擷取dom 工廠
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
docbf.setNamespaceAware(true);
//擷取dom一個執行個體
try {
DocumentBuilder docB = docbf.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL);
document = docB.parse(inputStream);
NodeList nodeList = document.getElementsByTagName("string");
int leng = nodeList.getLength();
for (int i = 0; i < leng; i++) {
Node node = nodeList.item(i);
String result = node.getFirstChild().getNodeValue();
String[] address = result.split(",");
String pName = address[0];
String pCode = address[1];
if(proinceName.equals(pName)){
proinceId = Integer.parseInt(pCode);
}
}
inputStream.close(); //關閉輸入資料流
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return proinceId;
}
/**
* 擷取市id
* @param provinceCode 省份id
* @param city 城市名稱
* */
public static int getCityCode(int provinceCode,String city){
int cityCode =0; //城市id
Document document ;
//解析xml
//擷取dom 工廠
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
docbf.setNamespaceAware(true);
//擷取dom一個執行個體
try {
DocumentBuilder docB = docbf.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(CITY_CODE_URL+provinceCode);
document = docB.parse(inputStream);
NodeList nodeList = document.getElementsByTagName("string");
int leng = nodeList.getLength();
for (int i = 0; i < leng; i++) {
Node node = nodeList.item(i);
String result = node.getFirstChild().getNodeValue();
String[] address = result.split(",");
String cName = address[0];
String cCode = address[1];
if(city.equals(cName)){
cityCode = Integer.parseInt(cCode);
}
}
inputStream.close(); //關閉輸入資料流
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cityCode;
}
/**
* 擷取天氣資訊
* @param cityCode 城市的Id
* @return 天氣的資訊
* */
public static List<String> getWeather(int cityCode){
List<String> weatherList = new ArrayList<String>();
Document document ;
//解析xml
//擷取dom 工廠
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
docbf.setNamespaceAware(true);
//擷取dom一個執行個體
try {
DocumentBuilder docB = docbf.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL+cityCode);
document = docB.parse(inputStream);
NodeList nodeList = document.getElementsByTagName("string");
int leng = nodeList.getLength();
for (int i = 0; i < leng; i++) {
Node node = nodeList.item(i);
String result = node.getFirstChild().getNodeValue();
weatherList.add(result);
}
inputStream.close(); //關閉輸入資料流
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return weatherList;
}
}
建立servlet
public class WeatherServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String provinceName = new String(request.getParameter("provinceName").getBytes("ISO-8859-1"),"UTF-8");
String cityName = new String(request.getParameter("cityName").getBytes("ISO-8859-1"),"UTF-8");
int proinceCode = WeatherDemo.getProinceCode(provinceName);
System.out.println("省份ID:"+proinceCode);
int cityCode = WeatherDemo.getCityCode(proinceCode, cityName);
System.out.println("城市ID:"+cityCode);
List<String> strList = WeatherDemo.getWeather(cityCode);
System.out.println(strList.size());
request.setAttribute("strList", strList);
for(String in : strList){
System.out.println(in);
}
request.getRequestDispatcher("index.jsp").forward(request, response);
}
jsp代碼
<body>
<form action="WeatherServlet" method="post">
省份:<input type="text" id="provinceName" name="provinceName">
城市:<input type="text" id="cityName" name="cityName">
<input type="submit" value="查詢" >
</form>
<%= request.getAttribute("strList") %>
</body>
}
java擷取天氣預報的資訊