使用java获取未来7天天气信息,可用于android

这篇博客介绍了如何在Java环境下,利用Eclipse和JDK1.6,不依赖第三方库来获取并展示未来7天的天气信息。项目源码包含`WeatherUtil.java`、`WebPageUtil.java`、`WeatherReport.java`和测试类`WeatherSample.java`。执行过程中,系统会生成`weathercn.xml`文件,存储区县和编号数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:eclipsse, jdk1.6, 没有使用第三方的包,都是JDK有的。

项目结构如下:


下载地址:

http://download.csdn.net/detail/wssiqi/5037597

注意,项目源文件我都使用的是UTF-8的编码格式,如果不是,会显示乱码。

设置UTF-8:windows->Preferences->General->Workspace 页面上Text file encoding,选择Other UTF-8

1.获取天气预报的类

WeatherUtil.java

package com.siqi.weather;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
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.Element;
import org.w3c.dom.NodeList;

/**
 * 中国气象频道手机版("http://m.weathercn.com")获取天气信息, 因为手机版网页内容小,访问速度快。
 * 
 * @author siqi
 * 
 */
public class WeatherUtil {

    /**
     * 省份页面(省)
     */
    public static final String PROVINCE_URL = "http://m.weathercn.com/common/province.jsp";
    /**
     * 城市页面(市)<br/>
     * pid=%s中%s表示城市编号 例:http://m.weathercn.com/common/dis.do?pid=010101
     */
    public static final String DISTRICT_URL = "http://m.weathercn.com/common/dis.do?pid=%s";
    /**
     * 县区页面(区)<br/>
     * did=%s中%s表示县区编号<br/>
     * pid=%s中%s表示城市编号<br/>
     * 例:http://m.weathercn.com/common/cout.do?did=01010101&pid=010101
     */
    public static final String COUNTY_URL = "http://m.weathercn.com/common/cout.do?did=%s&pid=%s";
    /**
     * 7天天气预报页面<br/>
     * cid=%s中%s表示区编号<br/>
     * 例:http://m.weathercn.com/common/7d.do?cid=0101010110
     */
    public static final String REPORT7_URL = "http://m.weathercn.com/common/7d.do?cid=%s";
    /**
     * 生活指数页面<br/>
     * cid=%s中%s表示区编号
     */
    public static final String REPORT_MORE_URL = "http://m.weathercn.com/common/zslb.do?cid=%s";

    /**
     * 保存城市编码信息的xml文档<br/>
     * 保存了具体的区县和区县所对应的编码,例如<br/>
     * <county><br/>
     * <name>北京</name><br/>
     * <code>0101010110</code><br/>
     * </county>
     */
    public static final String XML_FILE = "./weathercn.xml";

    private List<WeatherReport> weatherReportList = new ArrayList<WeatherReport>();

    /**
     * 启动的时候,首先检查weathercn.xml是否存在,如果不存在的话,重新从m.weathercn.com获取,
     * 只有第一次的时候会获取。
     */
    static {
        try {
            prepareXML();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回指定城市,指定日期的天气预报。<br/>
     * 
     * @param city
     *            城市,如"北京"。
     * @param cal
     *            日期。
     * @return 如果城市正确,并且日期在7天以内,那么返回天气信息。否则返回null。
     */
    public WeatherReport getWeatherReport(String city, Calendar cal) {
        String dateStr = cal.get(Calendar.MONTH) + "月"
                + cal.get(Calendar.DAY_OF_MONTH) + "日";
        return getWeatherReport(city, dateStr);
    }

    /**
     * 返回指定城市,指定日期的天气预报。<br/>
     * 
     * @param city
     *            城市,如"北京"。
     * @param date
     *            日期,格式为"1月20日"。
     * @return 如果城市正确,并且日期在7天以内,那么返回天气信息。否则返回null。
     */
    public WeatherReport getWeatherReport(String city, String date) {
        for (WeatherReport report : getWeatherReports(city)) {
            if (report.getDate().equals(date)) {
                return report;
            }
        }

        return null;
    }

    /**
     * 返回指定城市的天气预报(7天内)
     * 
     * @param city
     * @return 返回指定城市的天气预报(7天内),如果指定的城市错误,返回空的list,list.size()=0
     */
    public List<WeatherReport> getWeatherReports(String city) {
        List<WeatherReport> list = new ArrayList<WeatherReport>();
        try {

            String weatherPage = getWeatherReportPage(city);

            List<String> reportStrList = getAllMathers(weatherPage,
                    "(?<=class=\"b\">)[\\s\\S]+?<br>[\\s\\S]+?(?=</)");
            for (String reportStr : reportStrList) {
                String weather = reportStr.trim().replaceAll(" ", "")
                        .replaceAll("<br>\r\n\r\n", "\r\n")
                        .replaceAll("<br>", "");

                String[] str = weather.split("\r\n");
                if (str.length > 5) {
                    WeatherReport report = new WeatherReport();

                    int i = 0;
                    String dateStr = str[i++].trim();

                    report.setCity(city);
                    report.setDate(getMatcher(dateStr, ".+(?=\\()"));
                    report.setWeekDay(getMatcher(dateStr, "(?<=\\().+?(?=\\))"));
                    report.setDayOrNight(str[i++].trim());
                    report.setWeather(str[i++].trim());
                    report.setTemperature(str[i++].trim());
                    report.setWindDir(str[i++].trim());
                    report.setWind(str[i++].trim());

                    list.add(report);
                    if (str.length > 10) {
                        report = new WeatherReport();
                        report.setCity(city);
                        report.setDate(getMatcher(dateStr, ".+(?=\\()"));
                        report.setWeekDay(getMatcher(dateStr,
                                "(?<=\\().+?(?=\\))"));
                        report.setDayOrNight(str[i++].trim());
                        report.setWeather(str[i++].trim());
       
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值