http://www.oschina.net/code/snippet_993073_22264
2. [文件] Weather.java ~ 4KB 下载(334)
001 | package bizfuse.restful.svc.srpm.dataCollection; |
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; |
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; |
018 | import net.sf.json.JSONObject; |
024 | public class Weather { |
026 | URLConnection connectionData; |
032 | public Weather(String Cityid) throws IOException ,NullPointerException{ |
035 | this .Ctiyid = Cityid; |
037 | URL url = new URL( "http://m.weather.com.cn/data/" + Ctiyid + ".html" ); |
038 | connectionData = url.openConnection(); |
039 | connectionData.setConnectTimeout( 1000 ); |
041 | br = new BufferedReader( new InputStreamReader( |
042 | connectionData.getInputStream(), "UTF-8" )); |
043 | sb = new StringBuilder(); |
045 | while ((line = br.readLine()) != null ) |
047 | } catch (SocketTimeoutException e) { |
048 | System.out.println( "连接超时" ); |
049 | } catch (FileNotFoundException e) { |
050 | System.out.println( "加载文件出错" ); |
052 | String datas = sb.toString(); |
054 | jsonData = JSONObject.fromObject(datas); |
056 | info = jsonData.getJSONObject( "weatherinfo" ); |
059 | List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); |
060 | for ( int i= 1 ;i<= 6 ;i++){ |
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日" ); |
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()); |
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" )); |
093 | private String getWeek( int 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 ; |
108 | public static void main(String[] args) { |
110 | new Weather( "101010100" ); |
111 | } catch (Exception e) { |