天气预报的链接方式
来源:互联网 发布:域名授权.系统源码 编辑:程序博客网 时间:2024/06/05 07:21
/**
*
*/
package com.fw.util;
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 {
// http://www.ayandy.com
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;
}
}
- 天气预报的链接方式
- 关于天气预报可能用到的网上链接
- Oracle 的链接方式
- DLL的链接方式
- 分解链接的方式:
- 库的链接方式
- 程序的链接方式
- 通过xmlhttp的方式,利用qq天气预报,获得某一特定地方的天气预报
- Android获取天气预报的两种方式(国家气象局和中央气象台天气预报API)
- 常用的数据库链接方式
- 链接的2种方式
- yii 用到的链接方式
- 存储器--程序的链接方式
- Mysql数据库的链接方式
- 链接方式
- 利用中央气象台的天气预报API实现天气预报
- 多工程的lib的链接方式
- 程序的链接的三种方式
- BW:Maintaining Authorizations for Hierarchies
- 获取局域网外网Ip的方法
- BW:Debug Loop
- BB重启方法
- BW:What is meant by Primary Index and Secondary Index
- 天气预报的链接方式
- validate.js
- android binder 讲解(四)
- 邮件发送技巧:TO, CC, BCC(收件人、抄送、密送)
- 【初学者】立志成为程序员,写自己喜欢的代码。
- JAVA高级视频 网络编程 06 完善的TCP服务器程序模块
- Socket编程的面纱
- 显示桌面快速启动
- 有关Yii form 显示不出来 model找不到的问题