java调用WebService天气预报实例
来源:互联网 发布:java 程序员招聘 编辑:程序博客网 时间:2024/05/16 15:45
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 PROVINCE_CODE_URL = WEATHER_SERVICES_URL
+ "getRegionProvince";
private static String CITY_CODE_URL = WEATHER_SERVICES_URL
+ "getSupportCityString?theRegionCode=";
private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
+ "getWeather?theUserID=&theCityCode=";
private WeatherUtil(){}
public static void main(String[] args) throws Exception{
int provinceCode = getProvinceCode("山东"); //3119
int cityCode = getCityCode(provinceCode, "菏泽"); //974
List<String> weatherList = getWeather(cityCode);
for(String weather:weatherList){
System.out.println(weather);
}
}
public static int getProvinceCode(String provinceName){
Document document;
DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
documentBF.setNamespaceAware(true);
int provinceCode = 0;
try{
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL); //具体webService相关
document = documentB.parse(inputStream);
NodeList nodeList = document.getElementsByTagName("string"); //具体webService相关
int len = nodeList.getLength();
for(int i = 0; i < len; i++){
Node n = nodeList.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);
}
}
inputStream.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(CITY_CODE_URL + provinceCode); //具体webService相关
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string"); //具体webService相关
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;
}
public static 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 static List<String> getWeather(int 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();
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;
}
}
- java调用WebService天气预报实例
- java调用WebService天气预报实例
- java调用webservice 天气预报
- java调用webservice 天气预报
- java调用天气预报webservice
- java 调用天气预报的webservice
- Java 调用webservice 实现天气预报
- WebService通信调用天气预报,Java
- java调用天气预报的webservice
- webservice通信调用天气预报接口实例
- webservice通信调用天气预报接口实例
- webservice通信调用天气预报接口实例
- WebService之java调用天气预报案例
- 使用ksoap2 调用 WebService(实例:调用天气预报服务)
- 使用ksoap2 调用 WebService(实例:调用天气预报服务)
- 使用ksoap2 调用 WebService(实例:调用天气预报服务)
- java调用webservice实例
- java调用webservice实例
- freemarker使用struts2标签
- 利用python打造自己的ftp暴力破解工具
- STM32位带
- 二叉排序树的创建与使用
- 使用phantomjs进行刷商务通对话
- java调用WebService天气预报实例
- 记录下
- 郑大大一新生给我感觉
- poj刷题存档(1)
- linux内核学习——内存管理
- 液晶\串行\1602串口 (4bit)通讯
- Linux操作系统下的多线程编程详细解析----条件变量pthread_cond_t那些事儿
- Fedora和conio.h不得不说的故事(fedora缺少conio.h最强解决方案)
- addSubView的一点讨论