您的位置:首页 > 其它

WebService:新浪网天气预报接口实现

2012-09-04 16:30 381 查看
新浪网提供一个WebService的接口可以根据传入的参数反回.xml信息配置文件,在此提供以下接口实现:

package com.tjsoft.util;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class SinaGetWeather {

/** 新浪天气网址 */

public final String SINA_URL = "http://php.weather.sina.com.cn/xml.php";

/** 新浪天气XML调用密码 */

public final String PASSWORD = "DJOYnieT8234jlsK";

/** 获取天气的天数 */

static int[] day = { 0 };

/** 城市 */

public String[] city = { "北京", "天津", "上海", "重庆", "石家庄", "太原", "沈阳", "长春",

"哈尔滨", "南京", "杭州", "合肥", "福州", "南昌", "济南", "郑州", "武汉", "长沙", "广州",

"海口", "成都", "贵阳", "昆明", "西安", "兰州", "西宁", "拉萨", "南宁", "呼和浩特", "银川",

"乌鲁木齐", "香港", "台北", "澳门" };

/** 白天天气 */

public String status1;

/** 夜晚天气 */

public String status2;

/** 白天天气 拼音 */

public String figure1;

/** 夜晚天气拼音 */

public String figure2;

/** 白天风向 */

public String direction1;

/** 夜晚风向 */

public String direction2;

/** 白天风级 */

public String power1;

/** 夜晚风级 */

public String power2;

/** 白天温度 */

public String temperature1;

/** 夜晚温度 */

public String temperature2;

/** 体感温度 */

public String tgd;

/** 紫外线指数 */

public String zwx_l;

/** 紫外线说明 */

public String zwx_s;

/** 体感度指数 */

public String ssd_l;

/** 体感度说明 */

public String ssd_s;

/** 空调指数 */

public String ktk_l;

/** 空调说明 */

public String ktk_s;

/** 洗车指数 */

public String xcz_l;

/** 洗车说明 */

public String xcz_s;

/** 穿衣指数 */

public String chy_l;

/** 穿衣说明 */

public String chy_shuoming;

/** 污染物扩散条件 */

public String pollution_l;

/** 污染物扩散条件说明 */

public String pollution_s;

/** 感冒指数 */

public String gm_l;

/** 感冒说明 */

public String gm_s;

/** 运动指数 */

public String yd_l;

/** 运动说明 */

public String yd_s;

Element root;

// 获取天气函数

public String getweather() {

String tqinfo = "";

List<Weather> list = new ArrayList<Weather>();

for (int i = 0; i < day.length; i++) {

switch (day[i]) {

case 0:

System.out.println("================今天天气如下================");

break;

}

URL ur;

try {

// 获得DOM工厂对象

DocumentBuilderFactory domfac = DocumentBuilderFactory

.newInstance();

// javax.xml.parsers.DocumentBuilderFactory;类

DocumentBuilder dombuilder = domfac.newDocumentBuilder();

Document doc; // 创建文档对象

NodeList books;

// 循环访问获取各个地区不同天气情况

for (String str : city) {

ur = new URL(SINA_URL + "?city=" + str + "&password="

+ PASSWORD + "&day=" + day[i]);

doc = dombuilder.parse(ur.openStream());

root = doc.getDocumentElement();

books = root.getChildNodes();

for (Node node = books.item(1).getFirstChild(); node != null; node = node

.getNextSibling()) {

if (node.getNodeType() == Node.ELEMENT_NODE) {

if (node.getNodeName().equals("status1")) {

// 白天天气

status1 = node.getTextContent();

} else if (node.getNodeName().equals("status2")) {

// 夜晚天气

status2 = node.getTextContent();

} else if (node.getNodeName()

.equals("temperature1")) {

// 白天温度

temperature1 = node.getTextContent();

} else if (node.getNodeName()

.equals("temperature2")) {

// 夜晚温度

temperature2 = node.getTextContent();

} else if (node.getNodeName().equals("figure1")) {

// 白天天气 拼音

figure1 = node.getTextContent();

} else if (node.getNodeName().equals("figure2")) {

// 夜间 拼音

figure2 = node.getTextContent();

} else if (node.getNodeName().equals("direction1")) {

/** 白天风向 */

direction1 = node.getTextContent();

} else if (node.getNodeName().equals("power1")) {

/** 白天风级 */

power1 = node.getTextContent();

} else if (node.getNodeName().equals("power2")) {

/** 夜间风级 */

power2 = node.getTextContent();

} else if (node.getNodeName().equals("zwx_l")) {

/** 紫外线指数 */

zwx_l = node.getTextContent();

}

}

}

tqinfo += "<tr> <td scope='col'>" + str + "  "

+ status1 + "  温度:" + temperature1 + "℃~"

+ " " + temperature2 + "℃" + "  风向:"

+ direction1 + power1 + "-" + power2 + "</td></tr>";

System.out.println(tqinfo);

// tqinfo += "<ul>" + str + "   " + status1 + "

//   温度

//  "+ temperature1 + "℃~" + " " + temperature2+ "℃ " +

// "</ul> <br>";

}

} catch (Exception e) {

System.out.println("获取天气失败:请检查机器是否正常访问互联网");

}

}

return tqinfo;

}

}

测试类

package com.tjsoft.util;

public class SinaTest {

public static void main(String[] args) {

// 解析xml获得天气情况

SinaGetWeather gw = new SinaGetWeather();

gw.getweather();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: