Java get Google weather forecast---Store to local __java

Source: Internet
Author: User
Tags dateformat stringbuffer

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.Writer;
Import Java.net.URL;
Import Java.text.DateFormat;

Import Java.util.Date;
Import Javax.xml.parsers.DocumentBuilder;

Import Javax.xml.parsers.DocumentBuilderFactory;
Import org.w3c.dom.Document;

Import org.w3c.dom.NodeList; /** * @description Get Google's simple weather forecast via Java * @author Tianyu * @link http://weibo.com/10792402 * * public class Getgooglew
	 Eather {/** * * * @param cityname * Note that the pinyin written to the city is converted to a line, which is then extracted in XML format after it is opened.  * @return */public string GetWeather (string cityname, String fileaddr) {//Get weather on Google, write file try {URL url =
			New URL ("http://www.google.com/ig/api?hl=zh_cn&weather=" + cityname);
			InputStream InputStream = Url.openstream ();
			String s, str; BufferedReader in = new BufferedReader (new InputStreamReader (InputStream, "GB2312"))//Save settings encoded as gb2312 stringbuffer StringBuffer = new Stri
			
			Ngbuffer ();
			
			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 ();
		catch (IOException e) {e.printstacktrace ();
		//Read the required data file = new file (FILEADDR);
		Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
		String str = NULL;
			try {documentbuilder builder = Factory.newdocumentbuilder ();
			Document doc = builder.parse (file);
			NodeList Nodelist1 = (nodelist) doc. getElementsByTagName ("Forecast_conditions");

			NodeList nodelist2 = Nodelist1.item (0). Getchildnodes (); str = Nodelist2.item (4). GetAttributes (). Item (0). Getnodevalue () + ", Temperature:" + nodelist2.item (1). GetAttributes (). Item (0). GetnodevaLue () + "℃-" + Nodelist2.item (2). GetAttributes (). Item (0). Getnodevalue () + "℃";
		catch (Exception e) {e.printstacktrace ();
	return str;

		public static void Main (String args[]) {getgoogleweather GGW = new Getgoogleweather ();
		String cityname = "Beijing";
		String fileaddr = "f:/" + CityName + ". xml";

		String temperature = Ggw.getweather (CityName, fileaddr);
		Date nowdate = new Date ();
		DateFormat DateFormat = Dateformat.getdateinstance ();

		String today = Dateformat.format (nowdate);
	SYSTEM.OUT.PRINTLN (Today + "" + cityname + "weather conditions are:" + temperature);
 }
}

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.