java获取天气预报

http://www.oschina.net/code/snippet_993073_22264

1. [图片] 天气.jpg    

2. [文件] Weather.java ~ 4KB     下载(334)     

001 package bizfuse.restful.svc.srpm.dataCollection;
002  
003 import java.io.BufferedReader;
004 import java.io.FileNotFoundException;
005 import java.io.IOException;
006 import java.io.InputStreamReader;
007 import java.net.SocketTimeoutException;
008 import java.net.URL;
009 import java.net.URLConnection;
010 import java.text.SimpleDateFormat;
011 import java.util.ArrayList;
012 import java.util.Calendar;
013 import java.util.Date;
014 import java.util.HashMap;
015 import java.util.List;
016 import java.util.Map;
017  
018 import net.sf.json.JSONObject;
019 /**
020  * 得到未来6天的天气(含今天)
021  * @author Chewl
022  *
023  */
024 public class Weather {
025     String Ctiyid;
026     URLConnection connectionData;
027     StringBuilder sb;
028     BufferedReader br;// 读取data数据流
029     JSONObject jsonData;
030     JSONObject info;
031       
032     public Weather(String Cityid) throws IOException ,NullPointerException{
033         // 解析本机ip地址
034   
035         this.Ctiyid = Cityid;
036         // 连接中央气象台的API
037         URL url = new URL("http://m.weather.com.cn/data/" + Ctiyid + ".html");
038         connectionData = url.openConnection();
039         connectionData.setConnectTimeout(1000);
040         try {
041             br = new BufferedReader(new InputStreamReader(
042                     connectionData.getInputStream(), "UTF-8"));
043             sb = new StringBuilder();
044             String line = null;
045             while ((line = br.readLine()) != null)
046                 sb.append(line);
047         catch (SocketTimeoutException e) {
048             System.out.println("连接超时");
049         catch (FileNotFoundException e) {
050             System.out.println("加载文件出错");
051         }
052             String datas = sb.toString();  
053              
054            jsonData = JSONObject.fromObject(datas);
055           //  System.out.println(jsonData.toString()); 
056            info = jsonData.getJSONObject("weatherinfo");
057          
058         //得到1到6天的天气情况
059         List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
060         for(int i=1;i<=6;i++){
061             //得到未来6天的日期
062             Calendar cal = Calendar.getInstance();
063             cal.add(Calendar.DAY_OF_YEAR, i-1);
064             Date date = cal.getTime();
065             SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
066              
067             Map<String,Object> map = new HashMap<String, Object>();
068             map.put("city", info.getString("city").toString());//城市
069             map.put("date_y", sf.format(date));//日期
070             map.put("week", getWeek(cal.get(Calendar.DAY_OF_WEEK)));//星期
071             map.put("fchh", info.getString("fchh").toString());//发布时间
072             map.put("weather", info.getString("weather"+i).toString());//天气
073             map.put("temp", info.getString("temp"+i).toString());//温度
074             map.put("wind", info.getString("wind"+i).toString());//风况
075             map.put("fl", info.getString("fl"+i).toString());//风速
076             map.put("index", info.getString("index").toString());// 今天的穿衣指数
077             map.put("index_uv", info.getString("index_uv").toString());// 紫外指数
078             map.put("index_tr", info.getString("index_tr").toString());// 旅游指数
079             map.put("index_co", info.getString("index_co").toString());// 舒适指数
080             map.put("index_cl", info.getString("index_cl").toString());// 晨练指数
081             map.put("index_xc", info.getString("index_xc").toString());//洗车指数
082             map.put("index_d", info.getString("index_d").toString());//天气详细穿衣指数
083             list.add(map);
084         }
085         //控制台打印出天气
086        for(int j=0;j<list.size();j++){
087            Map<String,Object> wMap = list.get(j);
088            System.out.println(wMap.get("city")+"\t"+wMap.get("date_y")+"\t"+wMap.get("week")+"\t"
089                    +wMap.get("weather")+"\t"+wMap.get("temp")+"\t"+wMap.get("index_uv"));
090        }
091   
092     }
093     private String getWeek(int iw){
094         String weekStr = "";
095         switch (iw) {
096         case 1:weekStr = "星期天";break;
097         case 2:weekStr = "星期一";break;
098         case 3:weekStr = "星期二";break;
099         case 4:weekStr = "星期三";break;
100         case 5:weekStr = "星期四";break;
101         case 6:weekStr = "星期五";break;
102         case 7:weekStr = "星期六";break;
103         default:
104             break;
105         }
106         return weekStr;
107     }
108     public static void main(String[] args) {
109         try {
110             new Weather("101010100"); // 101010100(北京)就是你的城市代码
111         catch (Exception e) {
112             e.printStackTrace();
113         }
114     }
115 }


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值