JAVA调用天气预报服务WebService (webxml.com.cn网站提供)
2012-09-25 18:40
691 查看
好多人要老问我,今天上代码,Copy到工程就用了
package weather;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
public static String getYear(){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String time = format.format(calendar.getTime());
return time;
}
}
package 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.Date;
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 weather {
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 int CITICODE = 1954;
public static void main(String[] args) throws Exception {
String desc = "今天是" +DateUtils.getYear() + ","
+ DateUtils.getWeekOfDate(new Date());
desc += new weather().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 (weatherList != null && weatherList.size() > 7) {
String tianqi = weatherList.get(7);
if (tianqi.contains("日")) {
tianqi = tianqi.substring(tianqi.indexOf("日") + 1);
}
String wendu = weatherList.get(8);
desc += ",天气" + tianqi;
desc += " ,";
desc += wendu.replace("℃", "度").replace("/", "--");
}
} catch (Exception e) {
e.printStackTrace();
return desc;
}
return desc;
}
public 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;
}
}
下面是介绍
今天遇到的需求是显示一个“今天是星期五 2010年9月26日 天气晴,19-25度”这样的网站提示信息。
需要用到公共的天气预报webservice。/**
*
*
*/
public class WeatherService {
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=";
/**
* 城市代码 / 武汉
*/
private static int CITICODE = 1582;
public static void main(String[] args) throws Exception {
String desc = "今天是星期" + DateUtils.getCurrentWeek() + " " + DateUtils.getCurrent("yyyy年MM月dd日") + " ";
desc += new WeatherService().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(weatherList != null && weatherList.size() > 7) {
String tianqi = weatherList.get(7);
if(tianqi.contains("日")) {
tianqi = tianqi.substring(tianqi.indexOf("日") + 1);
}
String wendu = weatherList.get(8);
desc += "天气" + tianqi;
desc += " ,";
desc += wendu.replace("℃", "度").replace("/", "-");
}
} catch(Exception e) {
e.printStackTrace();
return desc;
}
return desc;
}
public 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;
}
}
这里的citycode是城市代码,由于我只要用武汉的,这里已经定义好了,大家想找自己的城市,可以去这里查:
http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx
获得省份通过getRegionProvince,然后根据省IDgetSupportCityString获得CityCode
说下程序:
这个服务的返回结果是xml,如下:
<?xml version="1.0" encoding="UTF-8"?>
-
<ArrayOfString xmlns="http://WebXml.com.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<string>湖北</string>
<string>武汉</string>
<string>57494</string>
<string>57494.jpg</string>
<string>2012-7-11 16:32:58</string>
<string>29℃/35℃</string>
<string>7月11日 多云</string>
<string>无持续风向微风</string>
<string>1.gif</string>
<string>1.gif</string>
<string>今日天气实况:气温:34℃;风向/风力:西南风 3级;湿度:60%;空气质量:较差;紫外线强度:中等</string>
<string>穿衣指数:天气炎热,建议着短衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫等清凉夏季服装。
感冒指数:各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。
运动指数:天气较好,无雨水困扰,但考虑气温很高,请注意适当减少运动时间并降低运动强度,运动后补充水分。
洗车指数:较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。
晾晒指数:天气不错,适宜晾晒。赶紧把久未见阳光的衣物搬出来吸收一下太阳的味道吧!
旅游指数:多云,同时有微风相伴,但温度较高,天气热,请尽量避免高温时段外出,若外出请注意防暑降温和防晒。
路况指数:天气较好,路面比较干燥,路况较好,不过天气有点热,定期让车辆休息。
舒适度指数:天气炎热,且空气湿度较大,会使您感到很闷热,很不舒适。
空气污染指数:气象条件较不利于空气污染物稀释、扩散和清除,请适当减少室外活动时间。
紫外线指数:属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。</string>
<string>26℃/34℃</string>
<string>7月12日 阴转小雨</string>
<string>无持续风向微风</string>
<string>2.gif</string>
<string>7.gif</string>
<string>25℃/30℃</string>
<string>7月13日 大雨转小雨</string>
<string>无持续风向微风</string>
<string>9.gif</string>
<string>7.gif</string>
<string>武汉市位于江汉平原东部,长江中游与长江、汉水交汇处。东经113°41′-115°05′,北纬29°58′-31°22′。武汉市地理位置优越,长江及其最大支流汉江交汇于此,将武汉市区天然分成汉口、汉阳和武昌三镇,武汉是我国水陆交通枢纽,控长江中游之咽喉,扼南北交通之要冲,素有“九省通衢”之称,现全市货运吞吐量达亿吨以上。优越的地理位置,成为历代兵家争夺的战略要地。三国时,武汉东湖附近曾是刘备、孙权、曹操进行军事、政治活动的场所,现在留下的有刘备郊天台、吴王庙、曹操庙、洪山宝塔等古建筑。武汉现已发展为中国中部地区工业、金融、商业、科学、文化教育中心。武汉市属亚热带湿润季风气候,雨量充沛、日照充足,四季分明。总体气候环境良好,近几年30年来,年均降雨量1269毫米,且多集中在6-8月。年均气温15.8℃-17.5℃,年无霜期一般为211天-272天,年日照总时数1810小时-2100小时。景观:武当山、长江三峡、神农架等。</string>
</ArrayOfString>
由于这里我只需要显示今天的天气,所以对string提取了7和8那两段进行处理。
测试:执行main方法,结果如下:
今天是星期三 2012年07月11日 天气 多云 ,29度-35度
相关文章推荐
- WebService调用http://www.webxml.com.cn中的一些服务示例
- C#调用WebService实现天气预报 http://www.webxml.com.cn
- 推荐一个提供Web Service的网站 http://www.webxml.com.cn
- 调用http://WebXml.com.cn/的webservice获取手机号段信息
- WebService学习笔记-读取webxml.com.cn的天气服务信息
- WebService学习笔记-读取webxml.com.cn的天气服务信息
- 编写Web Serviceclient訪问www.webxml.com.cn提供的服务
- Java WebService 简单实例、调用第三方提供的webService服务
- 编写Web Service客户端访问www.webxml.com.cn提供的服务
- java.net.UnknownHostException: webservice.webxml.com.cn
- WebXml.com.cn 中国股票行情数据 WEB 服务(支持深圳和上海股市的全部基金、债券和股票),数据即时更新
- Web Service 一些对外公开的网络服务接口以及http://www.webxml.com.cn/zh_cn/index.aspx
- javaWeb服务详解【客户端调用】(含源代码,测试通过,注释) ——applicationContext.xml
- webService学习-调用第三方提供的webService服务实现获取天气预报信息
- java版的webservice,完全用java的正则表达式对websercice的xml进行解析,可以很快用到web中,只需要调用其中的封装好的方法
- 【WebService】调用第三方提供的webService服务(七)
- webxml.com.cn
- 收集几种解决:The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or t
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar