高手请看-》java做的引用软件引入天气预报 各位前辈,我初学java,做一个应用软件,想引入天气预报到我的界面里,请各位前辈给讲讲。越详细越好,我在这先谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 网上找来的代码,感觉还可以,希望对你有帮助import java.net.*;import javax.xml.parsers.*;import org.w3c.dom.*;public class GetWeather{ static String[] city = { "北京", "天津", "上海", "重庆", "石家庄", "太原", "沈阳", "长春", "哈尔滨", "南京", "杭州", "合肥", "福州", "南昌", "济南", "郑州", "武汉", "长沙", "广州", "海口", "成都", "贵阳", "昆明", "西安", "兰州", "西宁", "拉萨", "南宁", "呼和浩特", "银川", "乌鲁木齐", "香港", "台北", "澳门" }; // 各个城市 static int[] day = { 0, 1, 2, 3, 4 }; // 哪一天的天气 static String weather; // 保存天气情况 static String high; // 保存当天最高温度 static String low; // 保存当天最低温度 public void getweather() // 获取天气函数 { URL ur; try { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); // 相关这个类的使用,可以去网上搜索,下同,不做详细介绍 DocumentBuilder dombuilder = domfac.newDocumentBuilder(); Document doc; Element root; NodeList books; for (String str : city) { // 循环访问获取各个地区不同天气情况 ur = new URL("http://php.weather.sina.com.cn/xml.php?city=" + str + "&password=DJOYnieT8234jlsK&day=" + day); 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")) weather = node.getTextContent(); // 获取到天气情况 else if (node.getNodeName().equals("temperature1")) high = node.getTextContent(); // 获取到最高温度 else if (node.getNodeName().equals("temperature2")) low = node.getTextContent(); // 获取到最低温度 } } System.out.println(str + " " + weather + " " + low + "℃~" + high + "℃"); // 前台输出 } } catch (Exception e) { System.out.println("获取天气失败:" + e); } } public static void main(String[] arg) { new GetWeather().getweather(); // 主接口函数调用执行方法 }} 这个要会web services调用气象部门所提供的天气接口。http://blog.csdn.net/lxqluo/article/details/6968599 大家帮看看,很简单的东西,继承覆盖,在线求 如何处理 HeadlessException ? filter过滤器配置如何不过滤一些页面 JRidioButton怎样插入到SQL Server2000数据库 gtlang78()请进 求值 大虾们,这个程序怎么会输出这个结果啊,参与有分! 如何从控制台读入字符‘^’ 小程序访问数据库在IE下运行为何没有效果? setIcon后按鈕上的圖片沒變,請指教! 数组的移除问题? Java类的放置顺序
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;public class GetWeather
{
static String[] city = { "北京", "天津", "上海", "重庆", "石家庄", "太原", "沈阳", "长春", "哈尔滨", "南京", "杭州", "合肥", "福州", "南昌", "济南", "郑州", "武汉", "长沙", "广州", "海口", "成都", "贵阳", "昆明", "西安", "兰州", "西宁", "拉萨", "南宁", "呼和浩特", "银川", "乌鲁木齐", "香港", "台北", "澳门" }; // 各个城市
static int[] day = { 0, 1, 2, 3, 4 }; // 哪一天的天气
static String weather; // 保存天气情况
static String high; // 保存当天最高温度
static String low; // 保存当天最低温度 public void getweather() // 获取天气函数
{
URL ur; try
{ DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); // 相关这个类的使用,可以去网上搜索,下同,不做详细介绍
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
Document doc;
Element root;
NodeList books;
for (String str : city)
{ // 循环访问获取各个地区不同天气情况
ur = new URL("http://php.weather.sina.com.cn/xml.php?city=" + str + "&password=DJOYnieT8234jlsK&day=" + day);
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"))
weather = node.getTextContent(); // 获取到天气情况
else if (node.getNodeName().equals("temperature1"))
high = node.getTextContent(); // 获取到最高温度
else if (node.getNodeName().equals("temperature2"))
low = node.getTextContent(); // 获取到最低温度
}
}
System.out.println(str + " " + weather + " " + low + "℃~" + high + "℃"); // 前台输出
} }
catch (Exception e)
{
System.out.println("获取天气失败:" + e);
}
} public static void main(String[] arg)
{
new GetWeather().getweather(); // 主接口函数调用执行方法
}}
这个要会web services调用气象部门所提供的天气接口。
http://blog.csdn.net/lxqluo/article/details/6968599