WebService之java调用天气预报案例

来源:互联网 发布:java的replace 编辑:程序博客网 时间:2024/06/05 11:58
  • getRegionCountry 

    获得国外国家名称和与之对应的ID

    输入参数:无,返回数据:一维字符串数组。


  • getRegionDataset 

    获得中国省份、直辖市、地区;国家名称(国外)和与之对应的ID

    输入参数:无,返回数据:DataSet。


  • getRegionProvince 

    获得中国省份、直辖市、地区和与之对应的ID

    输入参数:无,返回数据:一维字符串数组。


  • getSupportCityDataset 

    获得支持的城市/地区名称和与之对应的ID

    输入参数:theRegionCode = 省市、国家ID或名称,返回数据:DataSet。


  • getSupportCityString 

    获得支持的城市/地区名称和与之对应的ID

    输入参数:theRegionCode = 省市、国家ID或名称,返回数据:一维字符串数组。


  • getWeather 

    获得天气预报数据

    输入参数:城市/地区ID或名称,返回数据:一维字符串数组。


1、格式化日期DateUtils.java:


package com.hcw.weather;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtils {private static Date today = new Date();public static String getCurrentWeekOfDate() {        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};        Calendar cal = Calendar.getInstance();        cal.setTime(today);        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;        if (w < 0)            w = 0;        return weekDays[w];    }public static String getCurrent(String string) {SimpleDateFormat dateFm = new SimpleDateFormat(string);return  dateFm.format(today);}public static void main(String[] args) {System.out.println(getCurrentWeekOfDate());System.out.println(getCurrent("yyyy年MM月dd日"));}}

2、调用天气预报的代码WeatherService.java:

package com.hcw.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 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=";    /**    *城市代码 / 赣州1846   *查看代码表:Znoe="江西" tID=1846 Area="赣州" CountryOrAreaName="赣州"   */   private static int CITYCODE = 1846; public static void main(String[] args) {String desc = "今天是" + DateUtils.getCurrentWeekOfDate() + " " + 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;   }    /**   * 控制显示的内容【根据实际需要返回想要的数据字符串】   * @return 天气相关的数据   */  public String getWeatherStr() {     String desc = "";     try {       List<String> weatherList = getWeather(CITYCODE);        //为了测试在此,打印出weather里面的所有数据,以后按需要截取相关的数据      for (int i = 0; i < weatherList.size(); i++) {System.out.println(i+ " = " + weatherList.get(i));}            if(weatherList != null && weatherList.size() > 7) {         String weather = weatherList.get(7);         if(weather.contains("日")) {         weather = weather.substring(weather.indexOf("日") + 1);         }         String wendu = weatherList.get(8);         desc += "天气" + weather;         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 = (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;   }  }

3、测试结果:

0 = 江西 赣州
1 = 赣州
2 = 1846
3 = 2013/04/07 22:08:37
4 = 今日天气实况:气温:15℃;风向/风力:北风 0级;湿度:68%
5 = 空气质量:良;紫外线强度:弱
6 = 穿衣指数:建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。
过敏指数:天气条件极易诱发过敏,有降水,易过敏人群尽量减少外出,如需外出最好穿长衣长裤,预防感冒可能引发的过敏。
运动指数:有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意携带雨具并注意避雨防滑。
洗车指数:不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。
晾晒指数:有降水,可能会淋湿晾晒的衣物,不太适宜晾晒。请随时注意天气变化。
旅游指数:有阵雨,温度适宜,在细雨中游玩别有一番情调,可不要错过机会呦!但记得出门要携带雨具。
路况指数:有降水,路面潮湿,车辆易打滑,请小心驾驶。
舒适度指数:温度适宜,风力不大,您在这样的天气条件下,会感到比较清爽和舒适。
空气污染指数:气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。
紫外线指数:紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。
7 = 4月8日 阵雨
8 = 13℃/22℃
9 = 无持续风向微风
10 = 3.gif
11 = 3.gif
12 = 4月9日 小雨转阵雨
13 = 9℃/18℃
14 = 无持续风向微风
15 = 7.gif
16 = 3.gif
17 = 4月10日 小雨
18 = 9℃/17℃
19 = 无持续风向微风
20 = 7.gif
21 = 7.gif
22 = 4月11日 小雨
23 = 9℃/15℃
24 = 无持续风向微风
25 = 7.gif
26 = 7.gif
27 = 4月12日 小雨
28 = 9℃/13℃
29 = 无持续风向微风
30 = 7.gif
31 = 7.gif
今天是星期日 2013年04月07日 天气 阵雨 ,13度-22度



原创粉丝点击