java調用 webservices接口實現天氣預報


最近要用到web services,而這兩天也比較有空,就弄了一個獲取天氣預報的Util。以前以為這有多難,因為數據來源是個困難。現在用web services的技術,發現下面這個是相當不錯的。
下面就用java把具體的代碼寫寫吧!
這里我采用比較簡單的get請求調用,畢竟這也沒什么秘密可言,就用最簡單的就可以了。
還有,這里很多捕獲異常的東西給我去掉了,自己加吧!

 1 public final class WeatherUtil
 2 {    
 3     private static String SERVICES_HOST = "www.webxml.com.cn";
 4     private static String WEATHER_SERVICES_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/";
 5     private static String SUPPORT_CITY_URL = WEATHER_SERVICES_URL
 6             + "getSupportCity?byProvinceName=ALL";
 7     private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
 8             + "getWeatherbyCityName?theCityName=";
 9     private WeatherUtil(){}
10     public static InputStream getSoapInputStream(String url)
11     {
12         InputStream is = null;
13 
14         URL U = new URL(url);
15         URLConnection conn = U.openConnection();
16         conn.setRequestProperty("Host", SERVICES_HOST);
17         conn.connect();
18         is = conn.getInputStream();
19         return is;
20     }
21     //取得支持的城市列表
22     public static ArrayList<String> getSupportCity()
23     {
24         ArrayList cityList = null;
25         Document doc;
26         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
27         dbf.setNamespaceAware(true);
28         DocumentBuilder db = dbf.newDocumentBuilder();
29         InputStream is = getSoapInputStream(SUPPORT_CITY_URL);
30         doc = db.parse(is);
31         NodeList nl = doc.getElementsByTagName("string");
32         int len = nl.getLength();
33         cityList = new ArrayList<String>(len);
34         for (int i = 0; i < len; i++)
35         {
36              Node n = nl.item(i);
37              String city = n.getFirstChild().getNodeValue();
38              cityList.add(city);
39         }
40         is.close();
41         return cityList;
42     }
43     //取得城市的天氣
44     public static ArrayList<String> getWeather(String city)
45     {
46         ArrayList weatherList = null;
47             Document doc;
48             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
49             dbf.setNamespaceAware(true);
50             DocumentBuilder db = dbf.newDocumentBuilder();
51 //這里他的編碼格式就是這樣,我試了幾個也沒辦法。。只好這樣混過去了
52             InputStream is = getSoapInputStream(WEATHER_QUERY_URL
53                     + new String(city.getBytes("UTF-8"), "GBK"));
54             doc = db.parse(is);
55             NodeList nl = doc.getElementsByTagName("string");
56             int len = nl.getLength();
57             weatherList = new ArrayList<String>(len);
58             for (int i = 0; i < len; i++)
59             {
60                 Node n = nl.item(i);
61                 String weather = n.getFirstChild().getNodeValue();
62                 weatherList.add(weather);
63             }
64             is.close();
65         return weatherList;
66     }
67  
68     public static void main(String[] args) throws Exception
69     {
70         ArrayList<String> weatherList = WeatherUtil.getWeather("59287");
71         // ArrayList<String> weatherList = WeatherUtil.getSupportCity();
72         for (String weather : weatherList)
73         {
74             System.out.println(weather);
75         }
76     }
77 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM