java调用天气预报的webservice
更多面試題請狠狠的點擊?下載
一、說明
天氣預報的webService地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
(天氣預報 Web 服務,數據每2.5小時左右自動更新一次,準確可靠。包括 340 多個中國主要城市和 60 多個國外主要城市三日內的天氣預報數據。
二、相關信息調用舉例以及說明
1、新建一個Java類:WeatherUtil.java
package com.test.util;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.NET.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class WeatherUtil {
/**
*?對服務器端返回的XML文件流進行解析
*?
* @param city 用戶輸入的城市名稱
* @return ? ? ? ? ?字符串 用#分割
*/
public ?String getWeather(String city) {
try {
//使用Dom解析
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
//獲取調用接口后返回的流
InputStream is = getSoapInputStream(city);
doc = db.parse(is);
//xml的元素標簽是"<string>值1</string><string>值2</string>……"
NodeList nl = doc.getElementsByTagName("string");
StringBuffer sb = new StringBuffer();
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if(n.getFirstChild().getNodeValue().equals("查詢結果為空!")) {
sb = new StringBuffer("#") ;
break ;
}
//解析并以"#"為分隔符,拼接返回結果
sb.append(n.getFirstChild().getNodeValue() + "#");
}
is.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/*
*?用戶把SOAP請求發送給服務器端,并返回服務器點返回的輸入流
*?
* @param city ?用戶輸入的城市名稱
* @return 服務器端返回的輸入流,供客戶端讀取
* @throws Exception
* @備注:有四種請求頭格式1、SOAP 1.1; ?2、SOAP 1.2 ; 3、HTTP GET; 4、HTTP POST
*?參考---》http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName
*/
private ?InputStream getSoapInputStream(String city) throws Exception {
try {
//獲取請求規范
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
//調用的天氣預報webserviceURL
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
//調用的接口方法是“getWeatherbyCityName”
conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
//獲取webserivce返回的流
InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/*
*?獲取SOAP的請求頭,并替換其中的標志符號為用戶輸入的城市
*?
* @param city: 用戶輸入的城市名稱
* @return ? ? ? ? ? ? ?客戶將要發送給服務器的SOAP請求規范
* @備注:有四種請求頭格式1、SOAP 1.1; ?2、SOAP 1.2 ; 3、HTTP GET; 4、HTTP POST
*?參考---》http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName
* 本文采用:SOAP 1.1格式
*/
private ?String getSoapRequest(String city) {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body><getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
+ "<theCityName>"?
+ city
+ "</theCityName></getWeatherbyCityName>"
+ "</soap:Body></soap:Envelope>");
return sb.toString();
}
}
注:webservice返回的xml文件如下
2、新建一個測試類:TestWeather.java
package com.util;
import com.test.util.WeatherUtil;
public class TestWeather {
/**
*?測試
*/
public static void main(String[] args) throws Exception {
WeatherUtil weath=new WeatherUtil();
//查看城市:濟南
String weather=weath.getWeather("濟南");
String len[]=weather.split("#");
for(int i=0;i<len.length-1;i++){
System.out.println(len[i]);
}
/**
* 打印結果:
* 山東
* 濟南
* 54823
* 54823.jpg
??* 2016-3-5 13:52:01
* 4℃/14℃
* 3月5日 多云轉晴
* 北風3-4級轉南風微風
* 1.gif
* 0.gif
* 今日天氣實況:氣溫:12℃;風向/風力:東北風 2級;濕度:23%;紫外線強度:弱。空氣質量:良。
* 紫外線指數:弱,輻射較弱,涂擦SPF12-15、PA+護膚品。
? ? ? 感冒指數:極易發,強降溫,濕度較大,極易感冒。
? ? ? 穿衣指數:較冷,建議著厚外套加毛衣等服裝。
? ? ? 洗車指數:較適宜,無雨且風力較小,易保持清潔度。
? ? ? 交通指數:良好,氣象條件良好,車輛可以正常行駛。
空氣污染指數:良,氣象條件有利于空氣污染物擴散。
? * 7℃/17℃
* 3月6日 多云
* 南風微風
* 1.gif
* 1.gif
* 4℃/16℃
* 3月7日 多云
* 南風微風轉北風3-4級
* 1.gif
* 1.gif
*/}
}
三、以上代碼已經測試通過,可以直接根據需要改動并使用
總結
以上是生活随笔為你收集整理的java调用天气预报的webservice的全部內容,希望文章能夠幫你解決所遇到的問題。