Import Java.io.bufferedreader;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.net.sockettimeoutexception;import Java.net.url;import Java.net.urlconnection;import Java.text.simpledateformat;import Java.util.arraylist;import Java.util.Calendar; Import Java.util.date;import java.util.hashmap;import Java.util.list;import Java.util.map; import Net.sf.json.jsonobject; public class Weather {String Cityid; URLConnection Connectiondata; StringBuilder SB; BufferedReader BR; Jsonobject Jsondata; Jsonobject info;public Weather (String cityid) throws Ioexception,nullpointerexception{this.cityid = Cityid; URL url = new URL ("http://m.weather.com.cn/data/" +cityid+ ". html"); Connectiondata = Url.openconnection (); Connectiondata.setconnecttimeout (+); try{br = new BufferedReader (New InputStreamReader ( Connectiondata.getinputstream (), "Utf-8"); sb = new StringBuilder (); String line = null;while (line = Br.readline ())! = null) sb.append (line);} catch (Sockettimeoutexception e) {System.out.println ("Connection timed Out");} catch (FileNotFoundException e) {System.out.println ("Error loading File");} String datas = sb.tostring (); jsondata = Jsonobject.fromobject (datas); info = jsondata.getjsonobject ("Weatherinfo"); List> List = new ArrayList> ();for (int i=1;i<=6;i++) {Calendar cal = Calendar.getinstance (); Cal.add (calendar.day_of_year,i-1);D ate Date = Cal.gettime (); SimpleDateFormat SF = new SimpleDateFormat ("yyyy mm month DD day"); Map map = new HashMap (), Map.put ("City", Info.getstring ("City"). ToString ()); Map.put ("Date_y", Sf.format (date)); Map.put ("Week", Getweek (Cal.get (Calendar.day_of_week))), Map.put ("Fchh", Info.getstring ("Fchh"). toString ()); Map.put ("Weather", info.getstring ("Weather" +i). ToString ()); Map.put ("Temp", info.getstring ("temp" + 1). ToString ()); Map.put ("Wind", Info.getstring ("Wind" + 1). ToString ()); Map.put ("FL", info.getstring ("FL" + 1). ToString ()); Map.put (" Index ", info.getstring (" index "). ToString ()); Map.put (" Index_uv ", Info.getstring (" Index_uv "). ToString ()); Map.put (" Index_tr ", info.getstring (" Index_tr "). ToString ()); Map.put (" Index_co ", Info.getstring (" Index_co "). ToString ()); Map.put ("Index_cl", Info.getstring ("Index_cl"). toString ()); Map.put ("Index_xc", Info.getstring ("Index_xc"). ToString ()); Map.put ("Index_d", Info.getstring ("Index_d "). toString ()); List.add (map);} for (int j=1;jmap wMap = List.get (j); System.out.println (Wmap.get ("City") + "\ T" +wmap.get ("date_y") + "\ T" +wmap.get ("Week") +wmap.get ("weather") + "\ T" + Wmap.get ("temp") + "\ T" +wmap.get ("Index_uv"));}} Public String getweek (int iw) {string weekstr = "", switch (IW) {case 1:weekstr = "Sunday"; break;case 2:weekstr = "Monday"; break;c ASE 3:weekstr = "Tuesday"; break;case 4:weekstr = "Wednesday"; break;case 5:weekstr = "Thursday"; break;case 6:weekstr = "Friday"; Break;case 7: Weekstr = "Saturday"; break;default:break;} return WEEKSTR;} public static void Main (string[] args) {try{new Weather ("101010100");//Beijing}catch (Exception e) {e.printstacktrace ();}}}
Get the weather forecast Java code