Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 369|回复: 0

[网络编程学习]未来6天的天气预报(java)

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-1 00:01:07 | 显示全部楼层 |阅读模式
    需求:获取未来6天的天气预报,条用中央气象台API来实现的
    有兴趣的同学可以,参考一下:代码如下
    1. import java.io.BufferedReader;  
    2. import java.io.FileNotFoundException;  
    3. import java.io.IOException;  
    4. import java.io.InputStreamReader;  
    5. import java.net.SocketTimeoutException;  
    6. import java.net.URL;  
    7. import java.net.URLConnection;  
    8. import java.text.SimpleDateFormat;  
    9. import java.util.ArrayList;  
    10. import java.util.Calendar;  
    11. import java.util.Date;  
    12. import java.util.HashMap;  
    13. import java.util.List;  
    14. import java.util.Map;  
    15.   
    16. import net.sf.json.JSONObject;  
    17.   
    18. public class Weather {  
    19.       
    20.   private String Cityid;  
    21.   private URLConnection connectionData;  
    22.   private StringBuilder sb;  
    23.   private BufferedReader br;// 读取data数据流   
    24.   private JSONObject jsonData;   
    25.   private JSONObject info;   
    26.       
    27.   public Weather(String cityid) throws IOException{  
    28.     //解析本机IP地址  
    29.     this.Cityid=cityid;  
    30.          
    31.     ///连接中央气象台的APi  
    32.     URL url=new URL("http://m.weather.com.cn/data/" + Cityid + ".html");  
    33.     connectionData = url.openConnection();   
    34.     connectionData.setConnectTimeout(1000);   
    35.     try {   
    36.        br = new BufferedReader(new InputStreamReader(connectionData.getInputStream(), "UTF-8"));   
    37.        sb = new StringBuilder();   
    38.        String line = null;   
    39.        while ((line = br.readLine()) != null){  
    40.            sb.append(line);   
    41.        }  
    42.    } catch (SocketTimeoutException e) {   
    43.         System.out.println("连接超时");   
    44.    } catch (FileNotFoundException e) {   
    45.         System.out.println("加载文件出错");   
    46.    }     
    47.               
    48.      String datas = sb.toString();     
    49.               
    50.      jsonData = JSONObject.fromObject(datas);   
    51.      //  System.out.println(jsonData.toString());   
    52.      info = jsonData.getJSONObject("weatherinfo");   
    53.                   
    54.      //得到1到6天的天气情况  
    55.      List< Map< String,Object>> list =new ArrayList< Map< String,Object>>();  
    56.      for(int i=1;i<=6;i++){  
    57.         //得到未来6天的日期  
    58.         Calendar cal = Calendar.getInstance();  
    59.         cal.add(Calendar.DAY_OF_YEAR, i-1);  
    60.         Date date = cal.getTime();  
    61.         SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");  
    62.                   
    63.         Map< String,Object> map = new HashMap< String, Object>();  
    64.         map.put("city", info.getString("city").toString());//城市  
    65.         map.put("date_y", sf.format(date));//日期  
    66.         map.put("week", getWeek(cal.get(Calendar.DAY_OF_WEEK)));//星期  
    67.         map.put("fchh", info.getString("fchh").toString());//发布时间  
    68.         map.put("weather", info.getString("weather"+i).toString());//天气  
    69.         map.put("temp", info.getString("temp"+i).toString());//温度  
    70.         map.put("wind", info.getString("wind"+i).toString());//风况  
    71.         map.put("fl", info.getString("fl"+i).toString());//风速  
    72.         map.put("index", info.getString("index").toString());// 今天的穿衣指数   
    73.         map.put("index_uv", info.getString("index_uv").toString());// 紫外指数   
    74.         map.put("index_tr", info.getString("index_tr").toString());// 旅游指数   
    75.         map.put("index_co", info.getString("index_co").toString());// 舒适指数   
    76.         map.put("index_cl", info.getString("index_cl").toString());// 晨练指数   
    77.         map.put("index_xc", info.getString("index_xc").toString());//洗车指数   
    78.         map.put("index_d", info.getString("index_d").toString());//天气详细穿衣指数   
    79.           list.add(map);  
    80.         }         
    81.        //控制台打印出天气  
    82.        for(int j=0;j< list.size();j++){  
    83.          Map< String,Object> wMap = list.get(j);  
    84.          System.out.println(wMap.get("city")+"        "+wMap.get("date_y")+"        "+wMap.get("week")+"        "  
    85.          +wMap.get("weather")+"        "+wMap.get("temp")+"        "+wMap.get("index_uv"));  
    86.        }               
    87.      }
    88.       
    89.   private String getWeek(int iw){  
    90.     String weekStr = "";  
    91.     switch (iw) {  
    92.      case 1:weekStr = "星期天";break;  
    93.      case 2:weekStr = "星期一";break;  
    94.      case 3:weekStr = "星期二";break;  
    95.      case 4:weekStr = "星期三";break;  
    96.      case 5:weekStr = "星期四";break;  
    97.      case 6:weekStr = "星期五";break;  
    98.      case 7:weekStr = "星期六";break;  
    99.      default:  
    100.         break;  
    101.    }  
    102.               
    103.   return weekStr;              
    104. }  
    105.   
    106.   public static void main(String[] args) {   
    107.      try {   
    108.        new Weather("101010100"); // 101010100(北京)就是你的城市代码  
    109.        new Weather("101280101"); // 101280101(广州)就是你的城市代码  
    110.      } catch (Exception e) {   
    111.          e.printStackTrace();   
    112.       }   
    113.    }   
    114. }  
    复制代码


    用到的JAR文件;
    set classpath=.;c:javajarcommons-beanutils-1.8.3.jar;c:javajarcommons-collections-3.2.1.jar;c:javajarcommons-logging-1.1.1.jar;c:javajarezmorph-1.0.6.jar;c:javajarjson-lib-2.4-jdk15.jar;c:javajarcommons-lang-2.6.jar;

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/1/000106765.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-9-21 07:54 , Processed in 0.357197 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表