package weatherws;
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.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class WeatherUtil {
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 GET_CITY_CODE = WEATHER_SERVICES_URL
+ "getSupportCityString?theRegionCode=";
private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
+ "getWeather?theUserID=&theCityCode=";
private static String GET_REGION_BY_IP = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getGeoIPContext";
private static String GET_PROVINCE_CODE = WEATHER_SERVICES_URL
+ "getRegionProvince";
private WeatherUtil(){}
public static InputStream getSoapInputStream(String url)
{
InputStream is = null;
try {
URL U = new URL(url);
URLConnection conn = U.openConnection();
conn.setRequestProperty("Host", SERVICES_HOST);
conn.connect();
is = conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
public static List<String> getWeather(int cityCode)
{
List<String> weatherList = new ArrayList<String>();
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(WEATHER_QUERY_URL
+ cityCode);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++)
{
Node n = nl.item(i);
String weather = n.getFirstChild().getNodeValue();
weatherList.add(weather);
}
is.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;
}
public static void main(String[] args) throws Exception
{
getRegion();
String[] pInfo = getRegion();
String provinceName = pInfo[0];
String cityName = pInfo[1];
int provinceCode = getProvinceCode(provinceName);
int cityCode = getCityCode(provinceCode, cityName);
List<String> weatherList = WeatherUtil.getWeather(cityCode);
for (String weather : weatherList)
{
System.out.println(weather);
}
}
public static String[] getRegion()
{
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
String[] regionInfo = new String[5];
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(GET_REGION_BY_IP);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++)
{
if(i == 1)
{
Node n = nl.item(i);
String[] provinceInfo = n.getFirstChild().getNodeValue().split("省");
String province = provinceInfo[0];
String[] cityInfo = provinceInfo[1].split("市");
String city = cityInfo[0];
regionInfo[0] = province;
regionInfo[1] = city;
}
}
is.close();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return regionInfo;
}
public static int getProvinceCode(String provinceName)
{
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
int provinceCode = 0;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(GET_PROVINCE_CODE);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++)
{
Node n = nl.item(i);
String result = n.getFirstChild().getNodeValue();
String[] address = result.split(",");
String pName = address[0];
String pCode = address[1];
if(pName.equalsIgnoreCase(provinceName))
{
provinceCode = Integer.parseInt(pCode);
}
}
is.close();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return provinceCode;
}
public static int getCityCode(int provinceCode, String cityName)
{
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
int cityCode = 0;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(GET_CITY_CODE + provinceCode);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++)
{
Node n = nl.item(i);
String result = n.getFirstChild().getNodeValue();
String[] address = result.split(",");
String cName = address[0];
String cCode = address[1];
if(cName.equalsIgnoreCase(cityName))
{
cityCode = Integer.parseInt(cCode);
}
}
is.close();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return cityCode;
}
}
分享到:
相关推荐
### VB.NET与Java通过SOAP协议访问WebService的知识点 #### 一、基础知识介绍 - **SOAP (Simple Object Access Protocol)**: 是一种轻量级协议,用于交换结构化的信息。它定义了用于构造消息以及进行信息传输的...
Java Axis调用WebService服务端是Java开发者在进行分布式系统交互时常见的一种技术实践。Axis是Apache软件基金会开发的一个开源项目,它提供了一种方便、高效的方式,使得Java应用程序能够作为客户端来消费或作为...
标题“C++调用天气预报Webservice”涉及到的知识点主要包括以下几个方面: 1. **C++编程**:这是实现项目的基础,需要熟悉C++语法、面向对象编程以及异常处理等。 2. **Qt框架**:Qt库提供了丰富的UI组件,如按钮...
SOAP调用工具,如标题中的Webservice Studio 2.0,是专门用于处理SOAP消息的应用程序。这些工具允许用户构建和发送SOAP请求,然后显示相应的SOAP响应。通过这种方式,开发者可以测试Web服务接口是否按预期工作,调试...
在Java中,主要通过JAX-WS(Java API for XML Web Services)框架来实现WebService的发布和调用。本文将深入探讨如何使用JAX-WS来创建和使用WebService。 ### 1. JAX-WS发布WebService #### 1.1 创建服务类 在Java...
本篇将深入探讨如何使用C#来调用Weather Forecast(天气预报)的WebService源码,我们将探讨三种不同的调用方式。 ### 1. 使用SoapHttpClient(.NET Framework) 在.NET Framework中,调用WebService最常用的方法...
- **处理返回结果**:解析WebService服务器返回的SOAP消息,提取有用的数据。 #### 三、特殊场景:处理数组参数 - **数组参数处理**:当调用的方法需要传递数组类型的参数时,需要特别注意SOAP消息的构造。 - **...
当C#调用Java WebService时,这些类型需要被正确地映射到XML Schema的数据类型上,如`xsd:string`,以便在SOAP消息中进行传递。同样,整型数据在C#中的`System.Int32`和Java中的`java.lang.Integer`也需要被转换为`...
### Java WebService 简单实例 方式一(生成类文件方式) #### 一、概述 在本篇文章中,我们将详细介绍如何通过生成类文件的方式来创建一个简单的 Java WebService 应用程序。这种方式非常适合初学者理解和实践 ...
在这个例子中,`SoapResponse`是自定义的响应模型类,用于解析服务器返回的SOAP响应XML。 然后,创建一个OkHttp的拦截器,用于设置SOAPAction头信息,因为SOAP请求需要这个头来识别请求类型: ```java public class...
调用pb开发的webserver(SOAP 1.1) /* POST /webservice/n_webservice.asmx HTTP/1.1 Host: localhost Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <soap12:Envelope xmlns:xsi=...
### C# 使用 HttpWebRequest 调用 WebService 的方法详解 在C#开发中,有时候我们需要与WebService进行交互,获取或发送数据。本篇文章将详细介绍如何利用`HttpWebRequest`类来实现这一功能,并通过示例代码帮助...
### Java使用XFire调用WebService接口 #### 一、引言 随着互联网技术的发展,不同系统之间的通信变得越来越重要。WebService作为一种重要的分布式计算技术,在跨平台、跨语言的服务交互方面发挥着重要作用。本文将...
Java中的JAXB(Java Architecture for XML Binding)是一个用于在Java对象和XML之间进行绑定的工具,使得我们可以方便地在XML文档和Java对象之间进行转换。它简化了Web服务的开发,尤其是在处理XML数据时。本篇文章...
类似地,svcutil工具可以从WSDL和XSD(XML Schema Definition)文件生成服务代理类,这些类可以被用来动态调用Web服务。 【WebserviceAnalysis工具】 在给定的“WebserviceAnalysis”工具中,它显然提供了一个强大...
JavaScript调用WebService是一种常见的前后端通信方式,尤其在Web应用程序中,它允许客户端与服务器进行异步数据交换,实现动态内容的更新。本示例旨在详细介绍如何使用JavaScript调用WebService,以及涉及的相关...
【VB6调用WebService】是将经典的Visual Basic 6(VB6)编程环境与现代Web服务集成的技术。VB6虽然是一个较旧的开发工具,但它仍然在某些领域中被广泛使用,而调用WebService的能力使其能连接到更广泛的网络资源和...
调用pb开发的webserver(SOAP 1.2) /* POST /webservice/n_webservice.asmx HTTP/1.1 Host: localhost Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <soap12:Envelope xmlns:xsi=...
在“js调用webservice示例”中,可能采用的是ActiveXObject在IE-v11中调用Web Service,这是因为ActiveXObject是微软IE浏览器特有的,可以创建与服务器端对象的连接。以下是一个简单的示例: ```javascript var ...
1. 获取WebService的WSDL(Web Service Description Language)文件,它是WebService的接口定义。 2. 使用gSOAP工具或其他方式解析WSDL,生成对应的C++接口代码。 3. 在MFC项目中引入生成的代码,实例化客户端代理...