Java Gets the weather forecast information

Source: Internet
Author: User

Operating effect:

Main functions:

1,jsp page enter provinces and cities get local weather information based on conditions

2,java code utilizes third-party path addresses for provinces and cities

This project mainly realizes the Java obtains the weather forecast information
Steps
1, create the project Weatherdemo
2. Create Package structure
3, creating the class
4, access the third-party interface to open the host method
5, get the province ID method
6, get the city ID method
7. Ways to get the weather
8, writing the servlet
9, Release run

Java code

Creating the Weatherdemo Class

/**
* @version 1.0
* @author Ren
* The core interface of the weather forecast
* */
public class Weatherdemo {

private static String Servies_host = "www.webxml.com.cn"; Host address third-party

private static String Weather_service_url = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
ID of the province
private static String Province_code_url = Weather_service_url + "Getregionprovince";
The ID of the city
private static String City_code_url = Weather_service_url + "getsupportcitystring?theregioncode=";
The address of the weather
private static String Weather_query_url = Weather_service_url + "getweather?theuserid=&thecitycode=";

/**
* How to open the server host
* */
public static InputStream getsoapinputstream (String URL) {
InputStream inputstream = null;
try {
URL urlobj = new URL (URL);
URLConnection urlconn =urlobj.openconnection (); Open connection
Urlconn.setrequestproperty ("host", servies_host);//Access Host
Urlconn.connect ();
InputStream = Urlconn.getinputstream ();

} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}catch (IOException e) {
E.printstacktrace ();
}
return inputstream;
}

/**
* Get the code of the province
*
* */
public static int Getproincecode (String proincename) {
Org.w3c.dom.Document Document;
int Proinceid = 0; Province ID
Parsing xml
Get Dom Factory
Documentbuilderfactory DOCBF = Documentbuilderfactory.newinstance ();
Docbf.setnamespaceaware (TRUE);
Get an instance of DOM
try {
Documentbuilder DOCB = Docbf.newdocumentbuilder ();
InputStream InputStream = Getsoapinputstream (Province_code_url);
Document = Docb.parse (InputStream);
NodeList NodeList = document.getElementsByTagName ("string");
int leng = Nodelist.getlength ();
for (int i = 0; i < Leng; i++) {
Node node = Nodelist.item (i);
String result = Node.getfirstchild (). Getnodevalue ();
string[] address = Result.split (",");
String pName = address[0];
String pcode = address[1];
if (Proincename.equals (PName)) {
Proinceid = Integer.parseint (Pcode);
}
}
Inputstream.close (); Close the input stream
} catch (Parserconfigurationexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Saxexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

return Proinceid;
}

/**
* Get City ID
* @param Provincecode Province ID
* @param city Name
* */
public static int Getcitycode (Int. provincecode,string city) {

int citycode = 0; City ID
Document document;
Parsing xml
Get Dom Factory
Documentbuilderfactory DOCBF = Documentbuilderfactory.newinstance ();
Docbf.setnamespaceaware (TRUE);
Get an instance of DOM
try {
Documentbuilder DOCB = Docbf.newdocumentbuilder ();
InputStream InputStream = Getsoapinputstream (City_code_url+provincecode);
Document = Docb.parse (InputStream);
NodeList NodeList = document.getElementsByTagName ("string");
int leng = Nodelist.getlength ();
for (int i = 0; i < Leng; i++) {
Node node = Nodelist.item (i);
String result = Node.getfirstchild (). Getnodevalue ();
string[] address = Result.split (",");
String cName = address[0];
String ccode = address[1];
if (City.equals (CName)) {
Citycode = Integer.parseint (Ccode);
}
}
Inputstream.close (); Close the input stream
} catch (Parserconfigurationexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Saxexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return citycode;
}

/**
* Get weather information
* @param citycode City ID
* Information on @return weather
* */
public static list<string> getWeather (int citycode) {
list<string> weatherlist = new arraylist<string> ();
Document document;
Parsing xml
Get Dom Factory
Documentbuilderfactory DOCBF = Documentbuilderfactory.newinstance ();
Docbf.setnamespaceaware (TRUE);
Get an instance of DOM
try {
Documentbuilder DOCB = Docbf.newdocumentbuilder ();
InputStream InputStream = Getsoapinputstream (Weather_query_url+citycode);
Document = Docb.parse (InputStream);
NodeList NodeList = document.getElementsByTagName ("string");
int leng = Nodelist.getlength ();
for (int i = 0; i < Leng; i++) {
Node node = Nodelist.item (i);
String result = Node.getfirstchild (). Getnodevalue ();
Weatherlist.add (result);
}
Inputstream.close (); Close the input stream
} catch (Parserconfigurationexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Saxexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return weatherlist;

}

}

Create a servlet

public class Weatherservlet extends HttpServlet {

public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String provincename = new String (Request.getparameter ("Provincename"). GetBytes ("Iso-8859-1"), "UTF-8");
String cityname = new String (Request.getparameter ("CityName"). GetBytes ("Iso-8859-1"), "UTF-8");
int proincecode = Weatherdemo.getproincecode (provincename);
System.out.println ("Province ID:" +proincecode);
int citycode = Weatherdemo.getcitycode (Proincecode, CityName);
SYSTEM.OUT.PRINTLN ("City ID:" +citycode);
list<string> strlist = Weatherdemo.getweather (Citycode);
System.out.println (Strlist.size ());
Request.setattribute ("Strlist", strlist);
for (String in:strlist) {
System.out.println (in);
}
Request.getrequestdispatcher ("index.jsp"). Forward (request, response);
}

JSP code

<body>
<form action= "Weatherservlet" method= "POST" >
Province: <input type= "text" id= "Provincename" name= "Provincename" >
City: <input type= "text" id= "CityName" name= "CityName" >
<input type= "Submit" value= "inquiry" >
</form>
<%= Request.getattribute ("Strlist")%>
</body>

}

Java Gets the weather forecast information

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.