java获取天气预报的信息

运行效果:

技术分享

主要功能:

1,jsp页面输入省份和城市 根据条件获取当地的天气信息

2,java代码 利用第三方的省份和城市的路径地址

本工程主要实现java获取天气预报的信息
步骤
1,创建工程weatherDemo
2,创建包结构
3,创建类
4,访问第三方接口 打开主机方法
5,获取省份id方法
6,获取市id方法
7,获取天气的方法
8,编写servlet
9,发布运行

java代码

创建WeatherDemo类

/**
* @version 1.0
* @author ren
* 天气预报的核心接口
* */
public class WeatherDemo {

private static String SERVIES_HOST = "www.webxml.com.cn"; //主机地址 第三方的

private static String WEATHER_SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
//省份的id
private static String PROVINCE_CODE_URL = WEATHER_SERVICE_URL + "getRegionProvince";
//城市的id
private static String CITY_CODE_URL = WEATHER_SERVICE_URL + "getSupportCityString?theRegionCode=";
//天气的地址
private static String WEATHER_QUERY_URL = WEATHER_SERVICE_URL + "getWeather?theUserid=&theCityCode=";

/**
* 打开服务器主机的方法
* */
public static InputStream getSoapInputStream(String url){
InputStream inputStream = null;
try {
URL urlObj = new URL(url);
URLConnection urlConn =urlObj.openConnection(); //打开连接
urlConn.setRequestProperty("Host", SERVIES_HOST);//访问主机
urlConn.connect();
inputStream = urlConn.getInputStream();

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return inputStream;
}

/**
* 获取省份的编码
*
* */
public static int getProinceCode(String proinceName){
org.w3c.dom.Document document ;
int proinceId = 0; //省份id
//解析xml
//获取dom 工厂
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
docbf.setNamespaceAware(true);
//获取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(); //关闭输入流
} 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;
}

/**
* 获取市id
* @param provinceCode 省份id
* @param city 城市名称
* */
public static int getCityCode(int provinceCode,String city){

int cityCode =0; //城市id
Document document ;
//解析xml
//获取dom 工厂
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
docbf.setNamespaceAware(true);
//获取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(); //关闭输入流
} 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;
}

/**
* 获取天气信息
* @param cityCode 城市的Id
* @return 天气的信息
* */
public static List<String> getWeather(int cityCode){
List<String> weatherList = new ArrayList<String>();
Document document ;
//解析xml
//获取dom 工厂
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
docbf.setNamespaceAware(true);
//获取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(); //关闭输入流
} 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;

}

}

创建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("省份ID:"+proinceCode);
int cityCode = WeatherDemo.getCityCode(proinceCode, cityName);
System.out.println("城市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代码

<body>
<form action="WeatherServlet" method="post">
省份:<input type="text" id="provinceName" name="provinceName">
城市:<input type="text" id="cityName" name="cityName">
<input type="submit" value="查询" >
</form>
<%= request.getAttribute("strList") %>
</body>

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。