Get the weather forecast Java code

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.