Gets the weather information through the SOAP request and resolves the returned XML file.
Reference: Http://www.webxml.com.cn/WebServices/WeatherWS.asmx
ImportJava.io.InputStream; ImportJava.io.OutputStream; ImportJava.io.OutputStreamWriter; ImportJava.net.URL; Importjava.net.URLConnection; ImportJavax.xml.parsers.DocumentBuilder; Importjavax.xml.parsers.DocumentBuilderFactory; Importorg.w3c.dom.Document; ImportOrg.w3c.dom.Node; Importorg.w3c.dom.NodeList; /*** @ClassName weatherutils * @Description TODO Weather Information data source (http://www.webxml.com.cn/* Search by city or region name for the next three days weather conditions, current weather, weather and Life index: * The method is called as follows: input parameter: thecityname = City Chinese name (foreign city can be used in English) or city <span class= "Wp_ Keywordlink "style=" margin:0px; padding:0px; border:0px; background-color:transparent; " ><a Target=_blank href= "http://www.xuebuyuan.com/"title=" code "target=" _blank "style=" text-decoration:none; Color:rgb (1, 150, 227); " > Code </a></span> (do not enter the default is Shanghai), such as: Shanghai or 58367, if * City name repeat please use City Code query (available via getsupportcity or Getsupportdataset) ; Returns data: a one-dimensional array of String (22), with a total of * 23 elements. String (0) to String (4): Province, city, city code, city picture name, last updated time. String (5) to String (11): The temperature of the day, * overview, Wind direction and wind, weather trend start picture name (hereinafter: Icon one), weather trend end picture Name (hereinafter: Icon II), now the weather live, weather and life * index. String (12) to String (16): The next day's temperature, profile, wind direction and wind, icon one, icon two. String (17) to String (21): The third day of the temperature, * overview, Wind direction and wind, icon one, icon two. String (22) Description of the city or region being queried *@authorZyj [email protected] * @date 2013-10-14*/ Public classWeatherutils {/*** Gets the SOAP request header and replaces the city where the glyph is entered for the user * *@paramCity * User-entered name *@returnthe SOAP request that the customer will send to the server*/ Private Staticstring Getsoaprequest (String city) {StringBuilder sb=NewStringBuilder (); Sb.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>" + "<soap:envelope xmlns:xsi=\" http://www.w3.org /2001/xmlschema-instance\ "" + "xmlns:xsd=\" Http://www.w3.org/2001/xmlschema\ "" + "xmlns:soap=\" http://schemas.xmlsoap.org/soap/envelope/\ ">" + "<soap:Body> <getweat Her xmlns=\ "http://webxml.com.cn/\" > "+" <theCityCode> "+" City+ "</theCityCode> </getWeather>" + "</soap:Body></soap:Envelope>"); returnsb.tostring (); } /*** The user sends the SOAP request to the server side and returns the input stream returned by the server point * *@paramCity * User-entered name *@returninput stream returned by server side for client to read *@throwsException*/ Private StaticInputStream Getsoapinputstream (String city)throwsException {Try{String Soap=getsoaprequest (city); if(Soap = =NULL) { return NULL; } URL URL=NewURL ("Http://www.webxml.com.cn/WebServices/WeatherWS.asmx"); URLConnection Conn=url.openconnection (); Conn.setusecaches (false); Conn.setdoinput (true); Conn.setdooutput (true); Conn.setrequestproperty ("Content-length", integer.tostring (soap. Length ())); Conn.setrequestproperty ("Content-type", "text/xml; Charset=utf-8 "); Conn.setrequestproperty ("SOAPAction", "Http://WebXml.com.cn/getWeather"); OutputStream OS=Conn.getoutputstream (); OutputStreamWriter OSW=NewOutputStreamWriter (OS, "Utf-8")); Osw.write (SOAP); Osw.flush (); Osw.close (); InputStream is=Conn.getinputstream (); returnis ; } Catch(Exception e) {e.printstacktrace (); return NULL; } } /*** Parsing of XML returned by server side * *@paramCity * User-entered name *@returnstring with # split*/ Public Staticstring GetWeather (String city) {Try{Document doc; Documentbuilderfactory DBF=documentbuilderfactory.newinstance (); Dbf.setnamespaceaware (true); Documentbuilder DB=Dbf.newdocumentbuilder (); InputStream is=Getsoapinputstream (city); Doc=Db.parse (IS); NodeList NL= Doc.getelementsbytagname ("string"); StringBuffer SB=NewStringBuffer (); for(intCount = 0; Count < Nl.getlength (); count++) {Node n=Nl.item (count); if(N.getfirstchild (). Getnodevalue (). Equals ("The query result is empty! ") ) {SB=NewStringBuffer ("#") ; Break ; } sb.append (N.getfirstchild (). Getnodevalue ()+ "#"); } is.close (); returnsb.tostring (); } Catch(Exception e) {e.printstacktrace (); return NULL; } } /*** Test *@paramargs *@throwsException*/ Public Static voidMain (string[] args)throwsException {String weatherinfo= GetWeather ("Guangzhou"); System.out.println (Weatherinfo); } }
Java gets weather information via WebService interface