【个人收藏】
package com.weather;
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 final static String COUNTRY_CODE_URL=WEATHER_SERVICES_URL+"getRegionCountry";
private WeatherUtil() {
}
public static void main(String[] args) {
int RegionCountry =getRegionCountry("");
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 getRegionCountry(String provinceCountryName){
Document document;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
int provinceCode=0;
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(COUNTRY_CODE_URL);
document = db.parse(is);
NodeList nodeList = document.getElementsByTagName("string");
int length = nodeList.getLength();
for (int i=0; i < length; 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(provinceCountryName)) {
provinceCode = Integer.parseInt(pCode);
}
}
} catch (DOMException e){
// TODO Auto-generated catch block
System.out.println("Document Exception "+e.toString());
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
System.out.println("Parse Configuration Exception "+e.toString());
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
System.out.println("SAX Exception "+e.toString());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IO Exception "+e.toString());
e.printStackTrace();
}
return provinceCode;
}
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);
document = documentB.parse(inputStream);
NodeList nodeList = document.getElementsByTagName("string");
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);
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;
}
public static InputStream getSoapInputStream(String url) {
InputStream inputStream = null;
try {
URL urlObj = new URL(url);
URLConnection urlConn = urlObj.openConnection();
urlConn.setRequestProperty("Host", SERVICES_HOST);
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;
}
}
分享到:
相关推荐
a crawler to get weather data from
12 Build a Weather App - Use GPS and APIs to Get Data from the Web 2/2
12 Build a Weather App - Use GPS and APIs to Get Data from the Web 1/2
Get the data from Internet ("results":[["location": ("id":"WWGQDCW6TBW1","name":"天津",“country Read data Over! Print parsed data: City:天津 Weather:阴 Temp:15℃ UpdateTime:2023-03-20T12:20:14+08:
天气的API(Taiwan weather data API): 台湾地图SVG素材(Taiwan Map SVG): 参考的教学(Referencing Tutorials): (不包含API串接&Axios) 尝试使用的技术 基础技术:HTML+CSS 进阶技术:SVG的操作+SCSS+JQUARY+...
% curl http://openweathermap.org/data/2.0/weather/city/524901 | jsonget main.temp 259.92 或多个换行符分隔的值: % curl http://openweathermap.org/data/2.0/weather/city/524901 | jsonget name main.temp...
Get ready to unlock the power of your data. With the fourth edition of this comprehensive guide, you’ll learn how to build and maintain reliable, scalable, distributed systems with Apache Hadoop. ...
微信小程序之GET请求的实例详解 学习前端的人应该都会使用ajax的get请求数据,那么在微信小程序里怎样实现get请求呢?... url: 'http://apis.baidu.com/heweather/weather/free', data: {city:'bei
data = get_accuweather_forecast ( lat = latitude , long = longitude , api_key = ACCU_KEY ) 资料来源 这是一个很好的天气来源列表: 。 AccuWeather(完成) 天气公司(天气频道)(不会) 据我所知,该API...
$weather = file_get_contents(“http://www.weather.com.cn/data/sk/101280601.html”);echo $weather;?> 复制代码 代码如下:<html><head><meta http-equiv=”Content-Type” content=...
def get_Weather(situs): # situs:String类型,用户的最低级地址 # return data=[[min,max,wet]*3,pm2.5] ``` myKey.py 里放置了和风天气 api 的密钥 # 后端接口 接口地址:```/api/v1/weather/getweather/...
在开发网站的时候用到天气查询,由于是基于Wordpress的 所以有很多... $data = file_get_contents($url . $id .'.html'); $obj=json_decode($data); echo $obj->weatherinfo->city.':'.$obj->weatherinfo-
天气预报 ... 安装 使用软件包管理器安装节点模块。 npm install ... http . get ( url , function ( res ) { res . pipe ( downloadPath ) ; } ) ; // read and compare the downloaded data file an
天气日记应用程序项目 概述: ... 该项目需要创建一个异步Web... postData()=>通过POST路由接收来自GET请求和用户的传入数据,并将它们存储在端点(projectData)中。 updateUI()=>完成POST请求后,将动态更新UI。
What is Huginn? Huginn is a system for building agents that perform automated tasks for you online. They can read the web, watch for ...Track the weather and get an email when it's going to rain (or
Get to grips with the basics of neural networks and what they are used for Develop neural networks using hands-on examples Explore and code the most widely-used learning algorithms to make your neural...
添加一个GET路由,以服务器代码返回projectData对象。 添加一个将输入数据添加到projectData的POST路由。 从OpenWeatherMap网站获取API凭据。 链接一个发出POST请求的Promise,以将API数据以及用户输入的数据添加...
get_weather_data : 此功能可加载和清除给定NOAA站点ID和年份的天气数据。 它返回一个数据帧,其中包含空气温度,大气压力,风速和风向的时间序列。 plot_weather_data : 此功能可将气象站的观测结果可视化,包括
Get an introduction to parallel and distributed computing See synchronous and asynchronous programming Explore parallelism in Python Distributed application with Celery Python in the Cloud Python on ...