专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Google技术

jsp显示google 天气预报?解决办法

发布时间:2011-07-03 07:07:34 文章来源:www.iduyao.cn 采编人员:星星草
jsp显示google 天气预报??
Java code

package cn.vlabs.sso.vo.action;

import javax.xml.parsers.*;
import java.io.*;
import java.net.URL;
import java.text.DateFormat;
import java.util.Date;

import org.w3c.dom.*;

public class GetWeather {
    /**
     * 传入选入的城市
     */
    public String getWeather(String cityName, String fileAddr) {
        // 获取google上的天气情况,写入文件
        try {
            URL url = new URL("http://www.google.com/ig/api?hl=zh_cn&weather="
                    + cityName);
            InputStream inputstream = url.openStream();
            String s, str;
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    inputstream));
            StringBuffer stringbuffer = new StringBuffer();
            Writer out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileAddr), "utf-8"));
            while ((s = in.readLine()) != null) {
                stringbuffer.append(s);
            }
            str = new String(stringbuffer);
            out.write(str);
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 读取需要的数据
        File file = new File(fileAddr);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        String str = null;
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(file);
            NodeList nodelist1 = (NodeList) doc
                    .getElementsByTagName("forecast_conditions");
            NodeList nodelist2 = nodelist1.item(0).getChildNodes();
            str = nodelist2.item(4).getAttributes().item(0).getNodeValue()
                    + ",temperature:"
                    + nodelist2.item(1).getAttributes().item(0).getNodeValue()
                    + "℃-"
                    + nodelist2.item(2).getAttributes().item(0).getNodeValue()
                    + "℃";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    public static void main(String args[]) {
        GetWeather ggw = new GetWeather();
        String cityName = "beijing";
        String fileAddr = "F:/" + cityName + ".xml";
        String temperature = ggw.getWeather(cityName, fileAddr);

        Date nowDate = new Date();
        DateFormat dateformat = DateFormat.getDateInstance();
        String today = dateformat.format(nowDate);

        System.out.println(today + " " + cityName + "的天气情况是:" + temperature);
    }
}




这个类main方法可以把天气预报读出来 但我的问题是怎么显示到jsp页面中呢

他是xml 具体的还请大牛指点

------解决方案--------------------
把该方法写到一servlet里,在jsp里调用
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: