天气预报


/**
 * 来自http://www.webxml.com.cn/zh_cn/download_center.aspx
 * 根据用户的ip得到用户所在地的天气情况
 */
public class WeatherUtil {

    private static String SERVICES_HOST = "www.webxml.com.cn";
    private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
    private static String GET_CITY_CODE = WEATHER_SERVICES_URL
            + "getSupportCityString?theRegionCode=";

    private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
            + "getWeather?theUserID=&theCityCode=";

    private static String GET_REGION_BY_IP = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getGeoIPContext";

    private static String GET_PROVINCE_CODE = WEATHER_SERVICES_URL
            + "getRegionProvince";

    private WeatherUtil() {
    }

    /**
     * 得到访问网址后返回的信息
     *
     * @param url 访问的地址
     * @return 返回的字符流
     */
    public static InputStream getSoapInputStream(String url) {
        InputStream is = null;

        try {
            URL U = new URL(url);
            URLConnection conn = U.openConnection();
            conn.setRequestProperty("Host", SERVICES_HOST);
            conn.connect();
            is = conn.getInputStream();
        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
        return is;
    }

    /**
     * 返回天气预报信息
     *
     * @param cityCode 城市的代码
     * @return 天气预报信息
     */
    public static List<String> getWeather(int cityCode) {
        List<String> weatherList = new ArrayList<String>();
        Document doc;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);

        try {
            DocumentBuilder db = dbf.newDocumentBuilder();

            InputStream is = getSoapInputStream(WEATHER_QUERY_URL
                    + cityCode);
            doc = db.parse(is);
            NodeList nl = doc.getElementsByTagName("string");

            int len = nl.getLength();

            for (int i = 0; i < len; i++) {
                Node n = nl.item(i);
                String weather = n.getFirstChild().getNodeValue();
                weatherList.add(weather);
            }
            is.close();
        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();
        } catch (DOMException e) {

            e.printStackTrace();
        } catch (ParserConfigurationException e) {

            e.printStackTrace();
        } catch (SAXException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
        return weatherList;
    }

    public static void main(String[] args) throws Exception {


        String[] pInfo = getRegion();

        String provinceName = pInfo[0];
        String cityName = pInfo[1];

        int provinceCode = getProvinceCode(provinceName);

        int cityCode = getCityCode(provinceCode, cityName);

        System.out.println("provinceCode=" + provinceCode);
        System.out.println("cityCode=" + cityCode);

        List<String> weatherList = WeatherUtil.getWeather(cityCode);

        for (String weather : weatherList) {
            System.out.println(weather);
        }

    }

    /**
     * 得到省份名称和所在城市名称
     *
     * @return 地区信息
     */
    public static String[] getRegion() {
        Document doc;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        String[] regionInfo = new String[5];

        try {

            DocumentBuilder db = dbf.newDocumentBuilder();
            InputStream is = getSoapInputStream(GET_REGION_BY_IP);
            doc = db.parse(is);
            NodeList nl = doc.getElementsByTagName("string");
            int len = nl.getLength();

//            for (int i = 0; i < len; i++) {
//                if (i == 1) {
//                    Node n = nl.item(i);
//
//                    String nstr = n.getFirstChild().getNodeValue();
//                    if (nstr.indexOf("省") != -1) {
//
//                        String[] provinceInfo = n.getFirstChild().getNodeValue().split("省");
//                        String province = provinceInfo[0];
//                        String[] cityInfo = provinceInfo[1].split("市");
//
//                        String city = cityInfo[0];
//
//                        regionInfo[0] = province;
//                        regionInfo[1] = city;
//                    } else {
//                        String[] provinceInfo = n.getFirstChild().getNodeValue().split("市");
//                        String pro = provinceInfo[0];
//                        regionInfo[0] = pro;
//                        regionInfo[1] = pro;
//                    }
//                }
//
//            }

            regionInfo[0] = "河南";
            regionInfo[1] = "郑州";

            is.close();

        } catch (DOMException e) {

            e.printStackTrace();
        } catch (ParserConfigurationException e) {

            e.printStackTrace();
        } catch (SAXException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        return regionInfo;

    }

    /**
     * 得到省份的代码
     *
     * @param provinceName 省份的名称
     * @return 省份的代码
     */
    public static int getProvinceCode(String provinceName) {
        Document doc;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        int provinceCode = 0;

        try {

            DocumentBuilder db = dbf.newDocumentBuilder();
            InputStream is = getSoapInputStream(GET_PROVINCE_CODE);
            doc = db.parse(is);
            NodeList nl = doc.getElementsByTagName("string");
            int len = nl.getLength();

            for (int i = 0; i < len; i++) {
                Node n = nl.item(i);

                String result = n.getFirstChild().getNodeValue();

                System.out.println(i + ":" + result);

                String[] address = result.split(",");

                String pName = address[0];
                String pCode = address[1];

                if (pName.equalsIgnoreCase(provinceName)) {
                    provinceCode = Integer.parseInt(pCode);
                }

            }
            is.close();

        } catch (DOMException e) {

            e.printStackTrace();
        } catch (ParserConfigurationException e) {

            e.printStackTrace();
        } catch (SAXException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        return provinceCode;

    }

    /**
     * 得到城市的代码
     *
     * @param provinceCode 省份的代码
     * @param cityName     城市的名称
     * @return 城市的代码
     */
    public static int getCityCode(int provinceCode, String cityName) {
        Document doc;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        int cityCode = 0;

        try {

            DocumentBuilder db = dbf.newDocumentBuilder();
            InputStream is = getSoapInputStream(GET_CITY_CODE + provinceCode);
            doc = db.parse(is);

            NodeList nl = doc.getElementsByTagName("string");
            int len = nl.getLength();

            for (int i = 0; i < len; i++) {
                Node n = nl.item(i);

                String result = n.getFirstChild().getNodeValue();

                System.out.println(i + ":" + result);
                String[] address = result.split(",");

                String cName = address[0];
                String cCode = address[1];

                if (cName.equalsIgnoreCase(cityName)) {
                    cityCode = Integer.parseInt(cCode);
                }

            }
            is.close();

        } catch (DOMException e) {

            e.printStackTrace();
        } catch (ParserConfigurationException e) {

            e.printStackTrace();
        } catch (SAXException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        return cityCode;

    }


}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值