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>
}
java获取天气预报的信息的更多相关文章
- java获取当前操作系统的信息
java获取当前操作系统的信息 JavaOS虚拟机UnixEXT 从网上收集的一些关于java获取操作系统信息的方法,现在总结一下: 1获取本机的IP地址: private static Strin ...
- Java 获取到配置文件信息
Java程序将数据库或者服务器IP写入到代码中,难免缺少灵活性. 如果写入到配置文件,部署到不通服务器上,只需要修改配置文 件即可. Java怎么读取配置文件 /** * 获取到配置文件信息 * @p ...
- Java 获取所有子类信息
我以前的博客(Java Scala获取注解的类信息)介绍过通过Reflections工具通过使用特定注解的类的信息,其实本工具也可以获取接口,抽象类,类等的所有子类信息.使用方法如下: Reflect ...
- java获取类的信息
关键技术剖析 1.java.lang.reflect包实现了java的反射机制,在使用反射机制时,需要导入该包. 2.Class类的forName方法能够根据类名加载类,获得类的Class对象. Cl ...
- Java获取电脑硬件信息
package com.szht.gpy.util; import java.applet.Applet; import java.awt.Graphics; import java.io.Buffe ...
- Java获取系统相关信息System.getProperty()
java.version Java 运行时环境版本 java.vendor Java 运行时环境供应商 java.vendor.url Java 供应商的 URL java.home Java 安装目 ...
- JAVA获取系统相关信息
Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00003596 获取JRE系统相关参数 java 通过S ...
- java获取来访者mac信息
根据IP获取对应的Mac地址,支持win10+Linux package com.simonjia.util.other; /** * @Author: SimonHu * @Date: 2019/6 ...
- Java获取 JVM 运行信息
import java.lang.management.ClassLoadingMXBean; import java.lang.management.ManagementFactory; impor ...
随机推荐
- 10.15_SVG可以解决问题吗
(1)淘宝开放平台. (2)Teiid是一个数据虚拟化系统.Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架.Apache Jackrabbit. (3)SVG:百度百科.SVG.js .Sn ...
- [USACO1.2.2]方块转换 Transformations
P1205 [USACO1.2]方块转换 Transformations 标签 搜索/枚举 USACO 题目描述 一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方 ...
- linux ptheard 生产者消费者
; { { printf( pthread_mutex_lock(&mutex); != g_iBufSiz ...
- jQuery siblings()用法与实例。
jQuery 的遍历方法siblings() $("给定元素").siblings(".selected") 其作用是筛选给定的同胞同类元素(不包括给定元素本身 ...
- 降低IIScup使用率,提高性能
智能提醒webservice在高峰期间CPU使用率达到50%,内存消耗2G优化方法:启用IIS的Web Garden. 步奏如下: 在IIS7中,选择对应的应用程序池 然后右键高级设置. 把其中的最大 ...
- React新接触
html5页面的开始引入: <script src="../build/react.js"></script> <!--核心库--> < ...
- CentOS6.4 安装aria2多线程下载工具
aria2是一个Linux下的多线程下载工具,支持HTTP/HTTPS.FTP.BitTorrent.Metalink协议. 平时在linux上下载http上的东西常用如wget.curl命令,但是他 ...
- 从IT的角度思考BIM(三):敏捷开发
人们看到了远处BIM的美丽胜景和阻挡在眼前的宽广河流.有些人自信满满地跳入河中打算孤身游过彼岸,可是却失败了.有些人匆匆忙忙地造了船胡乱地滑向彼岸,可是也失败了. 要如何继续这段探索之旅? 无论是&l ...
- RSA算法原理及实现
参考资料: 阮哥的日志:http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html http://www.ruanyifeng ...
- Aircrack-ng官方文档翻译[中英对照]---Aireplay-ng
Aircrack-ng官方文档翻译---Aireplay-ng[90%] Description[简介] Aireplay-ng is used to inject frames. Aireplay- ...