1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 |
package
com.xzm.util.webservice; import
java.io.IOException; import
java.io.InputStream; import
java.io.UnsupportedEncodingException; import
java.net.MalformedURLException; import
java.net.URL; import
java.net.URLConnection; import
java.util.ArrayList; import
java.util.List; import
javax.xml.parsers.DocumentBuilder; import
javax.xml.parsers.DocumentBuilderFactory; import
javax.xml.parsers.ParserConfigurationException; import
org.springframework.util.StringUtils; import
org.w3c.dom.DOMException; import
org.w3c.dom.Document; import
org.w3c.dom.Node; import
org.w3c.dom.NodeList; import
org.xml.sax.SAXException; /** * 天气预报接口 * * @author xzm * */ public
class UserServiceClient { private
static String SERVICES_HOST = "www.webxml.com.cn" ; private
static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/" ; private
static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL + "getWeather?theUserID=&theCityCode=" ; /** * 城市代码 / 镇江: 1954 */ private
static String CITICODE = "青州" ; public
static void
main(String[] args) throws
Exception { String desc = new
UserServiceClient().getWeatherStr(); System.out.println(desc); } public
InputStream getSoapInputStream(String url) { InputStream inputStream = null ; try
{ URL urlObj = new
URL(url); URLConnection urlConn = urlObj.openConnection(); urlConn.setRequestProperty( "Host" , SERVICES_HOST); // 具体webService相关 urlConn.connect(); inputStream = urlConn.getInputStream(); } catch
(MalformedURLException e) { e.printStackTrace(); } catch
(IOException e) { e.printStackTrace(); } return
inputStream; } public
String getWeatherStr() { String desc = "" ; try
{ List<String> weatherList = getWeather(CITICODE); if (!StringUtils.isEmpty(weatherList)&&weatherList.size()> 7 ){ StringBuffer buffer = new
StringBuffer( "您查询的地区是:" ); buffer.append(weatherList.get( 0 )).append(weatherList.get( 1 )).append( " \n当前时间:" ).append(weatherList.get( 3 )).append( "\n" ) .append(weatherList.get( 4 )).append( "\n" ).append(weatherList.get( 5 )); desc=buffer.toString(); } else { desc = "哎呦,我脑袋笨,没查到。。" ; } } catch
(Exception e) { e.printStackTrace(); return
desc; } return
desc; } public
List<String> getWeather(String cityCode) { List<String> weatherList = new
ArrayList<String>(); Document document; DocumentBuilderFactory documentBF = DocumentBuilderFactory .newInstance(); documentBF.setNamespaceAware( true ); try
{ DocumentBuilder documentB = documentBF.newDocumentBuilder(); InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL + cityCode); document = documentB.parse(inputStream); NodeList nl = document.getElementsByTagName( "string" ); int
len = nl.getLength(); for
( int
i = 0 ; i < len; i++) { Node n = nl.item(i); String weather = n.getFirstChild().getNodeValue(); System.out.println(weather); weatherList.add(weather); } inputStream.close(); } catch
(UnsupportedEncodingException e) { e.printStackTrace(); } catch
(DOMException e) { e.printStackTrace(); } catch
(ParserConfigurationException e) { e.printStackTrace(); } catch
(SAXException e) { e.printStackTrace(); } catch
(IOException e) { e.printStackTrace(); } return
weatherList; } } |
原文:http://www.cnblogs.com/xuzhenmin/p/3578851.html