首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

雅虎天候

2012-07-23 
雅虎天气package com.weatherimport java.io.Fileimport java.io.FileOutputStreamimport java.io.IOEx

雅虎天气

package com.weather;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

import java.net.URLConnection;

import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

?

class GetWeatherInfo {

?? ?public static HashMap<String, String> cityCode = new HashMap<String, String>();

?? ?private final String[] dictionaryStrings = { "龙卷风", "热带风暴", "飓风", "强雷阵雨",

?? ? ? ? ? ?"雷阵雨", "小雨加雪", "雨加冰雹", "雪加冰雹", "冰雨", "毛毛雨", "冻雨", "阵雨", "阵雨", "小雪",

?? ? ? ? ? ?"零星小雪", "高吹雪", "雪", "冰雹", "雨夹雪", "尘", "雾", "薄雾", "多烟的", "大风", "有风",

?? ? ? ? ? ?"寒冷", "阴天", "夜间阴天", "白天阴天", "夜间多云", "白天多云", "夜间清亮", "晴朗", "转晴",

?? ? ? ? ? ?"转晴", "雨夹冰雹", "热", "雷阵雨", "雷阵雨", "雷阵雨", "雷阵雨", "大雪", "阵雪", "大雪",

?? ? ? ? ? ?"多云", "雷", "阵雪", "雷雨" };

?? ?public GetWeatherInfo() {

?? ? ? ?initCitys();

?? ?}

?? ?/* 初始化城市代号 */

?? ?private void initCitys() {

?? ? ? ?cityCode.put("北京", "2151330");

?? ? ? ?cityCode.put("天津", "2159908");

?? ? ? ?cityCode.put("武汉", "2163866");

?? ? ? ?cityCode.put("杭州", "2132574");

?? ? ? ?cityCode.put("合肥", "2127866");

?? ? ? ?cityCode.put("上海", "2151849");

?? ? ? ?cityCode.put("福州", "2139963");

?? ? ? ?cityCode.put("重庆", "29195242");

?? ? ? ?cityCode.put("南昌", "2133704");

?? ? ? ?cityCode.put("香港", "2165352");

?? ? ? ?cityCode.put("济南", "2168327");

?? ? ? ?cityCode.put("澳门", "1887901");

?? ? ? ?cityCode.put("郑州", "2172736");

?? ? ? ?cityCode.put("呼和浩特", "2149760");

?? ? ? ?cityCode.put("乌鲁木齐", "2143692");

?? ? ? ?cityCode.put("长沙", "2142699");

?? ? ? ?cityCode.put("银川", "2150551");

?? ? ? ?cityCode.put("广州", "2161838");

?? ? ? ?cityCode.put("拉萨", "2144789");

?? ? ? ?cityCode.put("海口", "2162779");

?? ? ? ?cityCode.put("南京", "2137081");

?? ? ? ?cityCode.put("成都", "2158433");

?? ? ? ?cityCode.put("石家庄", "2171287");

?? ? ? ?cityCode.put("贵阳", "2146703");

?? ? ? ?cityCode.put("太原", "2154547");

?? ? ? ?cityCode.put("昆明", "2160693");

?? ? ? ?cityCode.put("沈阳", "2148332");

?? ? ? ?cityCode.put("西安", "2157249");

?? ? ? ?cityCode.put("长春", "2137321");

?? ? ? ?cityCode.put("兰州", "2145605");

?? ? ? ?cityCode.put("西宁", "2138941");

?? ? ? ?cityCode.put("深圳", "2161853");

?? ?}

?? ?private Document getWeatherXML(String cityCode) throws IOException {

?? ? ? ?URL url = new URL("http://xml.weather.yahoo.com/forecastrss?w=" + cityCode + "&u=c");

?? ? ? ?URLConnection connection = url.openConnection();

?? ? ? ?Document Doc = stringToElement(connection.getInputStream());

?? ? ? ?return Doc;

?? ?}

?? ?/* 保存获取的天气信息XML文档 */

?? ?private void saveXML(Document Doc, String Path) {

?? ? ? ?TransformerFactory transFactory = TransformerFactory.newInstance();

?? ? ? ?Transformer transformer;

?? ? ? ?try {

?? ? ? ? ? ?transformer = transFactory.newTransformer();

?? ? ? ? ? ?DOMSource domSource = new DOMSource(Doc);

?? ? ? ? ? ?File file = new File(Path);

?? ? ? ? ? ?FileOutputStream out = new FileOutputStream(file);

?? ? ? ? ? ?StreamResult xmlResult = new StreamResult(out);

?? ? ? ? ? ?transformer.transform(domSource, xmlResult);

?? ? ? ?} catch (Exception e) {

?? ? ? ? ? ?System.out.println("保存文件出错!");

?? ? ? ?}

?? ?}

?? ?/* 获取天气信息 */

?? ?public String getWeather(String city) {

?? ? ? ?String result = null;

?? ? ? ?try {

?? ? ? ?String cityCode = GetWeatherInfo.cityCode.get(city);

?? ? ? ?System.out.println(cityCode);

?? ? ? ? ? ?Document doc = getWeatherXML(GetWeatherInfo.cityCode.get(city));

?? ? ? ? ? ?NodeList nodeList = doc.getElementsByTagName("channel");

?? ? ? ? ? ?for (int i = 0; i < nodeList.getLength(); i++) {

?? ? ? ? ? ? ? ?Node node = nodeList.item(i);

?? ? ? ? ? ? ? ?NodeList nodeList1 = node.getChildNodes();

?? ? ? ? ? ? ? ?for (int j = 0; j < nodeList1.getLength(); j++) {

?? ? ? ? ? ? ? ? ? ?Node node1 = nodeList1.item(j);

?? ? ? ? ? ? ? ? ? ?if (node1.getNodeName().equalsIgnoreCase("item")) {

?? ? ? ? ? ? ? ? ? ? ? ?NodeList nodeList2 = node1.getChildNodes();

?? ? ? ? ? ? ? ? ? ? ? ?for (int k = 0; k < nodeList2.getLength(); k++) {

?? ? ? ? ? ? ? ? ? ? ? ? ? ?Node node2 = nodeList2.item(k);

?? ? ? ? ? ? ? ? ? ? ? ? ? ?if (node2.getNodeName().equalsIgnoreCase(

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"yweather:forecast")) {

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NamedNodeMap nodeMap = node2.getAttributes();

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Node lowNode = nodeMap.getNamedItem("low");

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Node highNode = nodeMap.getNamedItem("high");

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Node codeNode = nodeMap.getNamedItem("code");

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String day = "今天";

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if (result == null) {

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?result = "";

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?} else {

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?day = "\n明天";

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?result = result

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?+ day

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?+ " "

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?+ dictionaryStrings[Integer

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.parseInt(codeNode

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.getNodeValue())]

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?+ "\t最低温度:" + lowNode.getNodeValue()

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?+ "℃ \t最高温度:" + highNode.getNodeValue()

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?+ "℃ ";

?? ? ? ? ? ? ? ? ? ? ? ? ? ?}else if(node2.getNodeName().equals("description")){

?? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println(node2.getFirstChild().getNodeValue());

?? ? ? ? ? ? ? ? ? ? ? ? ? ?String content = node2.getFirstChild().getNodeValue();

?? ? ? ? ? ? ? ? ? ? ? ? ? ?int a = content.indexOf("<br />");

?? ? ? ? ? ? ? ? ? ? ? ? ? ?String startStr = content.substring(0, a);

?? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println(startStr);

?? ? ? ? ? ? ? ? ? ? ? ? ? ?

?? ? ? ? ? ? ? ? ? ? ? ? ? ?

?? ? ? ? ? ? ? ? ? ? ? ? ? ?}else if(node2.getNodeName().equals("yweather:condition")){

?? ? ? ? ? ? ? ? ? ? ? ? ? ?NamedNodeMap nodeMap = node2.getAttributes();

?? ? ? ? ? ? ? ? ? ? ? ? ? ? Node codeNode = nodeMap.getNamedItem("code");

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Node dateNode = nodeMap.getNamedItem("date");

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Node tempNode = nodeMap.getNamedItem("temp");

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Node textNode = nodeMap.getNamedItem("text");

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("天气:"+dictionaryStrings[Integer.valueOf(codeNode.getNodeValue())-1]+",当前时间:"?

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? +dateNode.getNodeValue()+",气温(度):"+tempNode.getNodeValue()+",Weather Condition:"+textNode.getNodeValue());

?? ? ? ? ? ? ? ? ? ? ? ? ? ?}

?? ? ? ? ? ? ? ? ? ? ? ?}

?? ? ? ? ? ? ? ? ? ?}

?? ? ? ? ? ? ? ?}

?? ? ? ? ? ?}

?? ? ? ? ? ?//saveXML(doc, "E:\\Weather.xml");

?? ? ? ?} catch (Exception e) {

?? ? ? ? ? ?e.printStackTrace();

?? ? ? ?}

?? ? ? ?return result;

?? ?}

?? ?public Document stringToElement(InputStream input) {

?? ? ? ?try {

?? ? ? ? ? ?DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

?? ? ? ? ? ?Document doc = db.parse(input);

?? ? ? ? ? ?return doc;

?? ? ? ?} catch (Exception e) {

?? ? ? ? ? ?return null;

?? ? ? ?}

?? ?}

}

?

public class TestWeather {

?? ?public static void main(String arg[]) {

?? ? ? ?GetWeatherInfo info = new GetWeatherInfo();

?? ? ? ?String weather = info.getWeather("上海");

?? ? ? ?System.out.println(weather);

?? ?}

}


热点排行