Requirements: The title is actually very clear, the specific point is to use HTTP request Google Weather API, get a standard XML file, the XML file contains the weather data we need, then we are parsing this XML document to get the weather data.
Visit Google weather:
http://www.google.com/ig/api?hl=zh-cn&weather=,,, 30670000,104019996
30670000 for Latitude, 104019996 for longitude, this is the above XML in the latitude and longitude of the 10^6 (10 of six), the advantage of this method is that can be based on latitude and longitude to obtain weather forecasts, natural prediction areas can also be more accurate.
To access the links above, Google returns the following XML content:
<?xml version= "1.0"?>-<xml_api_reply version= "1" >-<weather module_id= "0" tab_id= "0" mobile_row= "0" mo bile_zipped= "1" row= "0" section= "0" >-<forecast_information> <city data= ""/> <postal_code data= ""/& Gt <latitude_e6 data= "30670000"/> <longitude_e6 data= "104019996"/> <forecast_date "2010-09-02" > <current_date_time data= "2010-09-02 15:00:00 +0000"/> <unit_system data= "SI"/> </forecast_ Information>-<current_conditions> <condition data= "Cloudy"/> <temp_f data= "/> <temp_c" "/>
From this XML file we can know that:<forecast_information> is the relevant information of the forecast,<current_conditions> is the current latitude and longitude of the weather, <forecast_ Conditions> predicts weather conditions in which node 1 is today's weather forecast for meteorology, followed by a forecast of three days of weather meteorology.
When you know the basics, you start coding.
The code is as follows:
Get Weather information
Public Weather GetWeather (string latitude_e6, String longitude_e6) throws RuntimeException {//Get weather on Google, write to file Strin G fileaddr = System.getproperty ("User.dir") + "/src/doc/" + Latitude_e6 + "_" + Longitude_e6 + ". xml"; Weather Weather = null; try {URL url = new URL ("http://www.google.com/ig/api?hl=zh_cn&weather=,,," + Latitude_e6 + "," + longitude_e6);//Access extension Late Thread.Sleep ((int) (Math.random () * 10) * 1000); InputStream InputStream = Url.openstream (); String s, str; BufferedReader in = new BufferedReader (new InputStreamReader (InputStream)); StringBuffer StringBuffer = new StringBuffer (); Writer out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (FILEADDR), "UTF-8")); while ((s = in.readline ())!= null) {stringbuffer.append (s);} str = new String (stringbuffer); Out.write (str); Out.close (); In.close (); Parse XML file to get weather information weather = Parseweather (FILEADDR); catch (Exception e) {e.printstacktrace (); Log log = Log.getlogger (); Log.logger.error ("Network connection failure or Connection timeout" (ConnectiOn timed out), unable to connect to Google API. ", e); return null; return weather; }
Parse XML file for weather information:
Public Weather Parseweather (String fileaddr) {//file address Weather Weather = null; File File = new file (FILEADDR); try {documentbuilderfactory factory = Documentbuilderfactory.newinstance (); Documentbuilder builder = Factory.newdocumentbuilder (); Document doc = builder.parse (file); Forecast_information node NodeList Fiele = (nodelist) doc.getelementsbytagname ("Forecast_information"); if (fiele.getlength () = = 0) {System.out.println ("================="); return null;} String City = Fiele.item (0). Getchildnodes (). Item (0). GetAttributes (). Item (0). Getnodevalue (); String postal_code = Fiele.item (0). Getchildnodes (). Item (1). GetAttributes (). Item (0). Getnodevalue (); String latitude_e6 = Fiele.item (0). Getchildnodes (). Item (2). GetAttributes (). Item (0). Getnodevalue (); String longitude_e6 = Fiele.item (0). Getchildnodes (). Item (3). GetAttributes (). Item (0). Getnodevalue (); String forecast_date = Fiele.item (0). Getchildnodes (). Item (4). GetAttributes (). Item (0). Getnodevalue (); String current_date_time = Fiele.item (0).Getchildnodes (). Item (5). GetAttributes (). Item (0). Getnodevalue (); String Unit_system = Fiele.item (0). Getchildnodes (). Item (6). GetAttributes (). Item (0). Getnodevalue (); Current_conditions node NodeList Ccele = (nodelist) doc.getelementsbytagname ("Current_conditions"); String condition = Ccele.item (0). Getchildnodes (). Item (0). GetAttributes (). Item (0). Getnodevalue (); String temp_f = Ccele.item (0). Getchildnodes (). Item (1). GetAttributes (). Item (0). Getnodevalue (); String Temp_c = Ccele.item (0). Getchildnodes (). Item (2). GetAttributes (). Item (0). Getnodevalue (); String humidity = ccele.item (0). Getchildnodes (). Item (3). GetAttributes (). Item (0). Getnodevalue (); String icon = Ccele.item (0). Getchildnodes (). Item (4). GetAttributes (). Item (0). Getnodevalue (); String wind_condition = Ccele.item (0). Getchildnodes (). Item (5). GetAttributes (). Item (0). Getnodevalue (); Forecast_conditions node String fcinfo = Getxmlbytag (fileaddr, "<forecast_conditions>", "</weather>"); String str = fcinfo.substring (0,fcinfo.indexof ("≪/weather> ")); Weather = new Weather (City,postal_code,latitude_e6,longitude_e6,forecast_date,current_date_time,unit_system, CONDITION,TEMP_F,TEMP_C,HUMIDITY,ICON,WIND_CONDITION,STR); catch (Exception e) {e.printstacktrace (); Log log = Log.getlogger (); Log.logger.error ("error", e); //delete XML file File.delete (); return weather; }
Reads the XML file start element to the end element:
public string Getxmlbytag (string filename,string begintag,string endtag) {String content = ""; try {fileinputstream fis = new FileInputStream (new File (filename)); String str = ""; InputStreamReader ISR = new InputStreamReader (FIS, "UTF-8"); BufferedReader in = new BufferedReader (ISR); String temp = ""; while (true) {temp = In.readline (), if (temp = null) break, str + temp;} in.close (); Content = str.substring (Str.indexof (Begintag), Str.indexof (Endtag) +endtag.length ()); The catch (Exception e) {System.out.println ("failed to read the file.") "); E.printstacktrace (); } return content; }
Here I have to get the weather data encapsulated as an entity weather, only to obtain the current latitude and longitude of the weather, the day and the forecast of the weather of three days did not do detailed packaging, according to their own needs can be encapsulated. Well, the operation is done.