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 ...
随机推荐
- Top命令查看内存
c 切换显示命令名称和完整命令行. M 根据驻留内存大小进行排序 第四行:内存状态 8306544k total — 物理内存总量(8GB) 7775876k used — 使用中的内存总量(7.7G ...
- 暑假集训(1)第七弹 -----Oil Deposits(Poj1562)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- 理解Java中的协变返回类型
在面向对象程序设计中,协变返回类型指的是子类中的成员函数的返回值类型不必严格等同于父类中被重写的成员函数的返回值类型,而可以是更 "狭窄" 的类型. Java 5.0添加了对协变返 ...
- 【转】Java编程之字符集问题研究
发现这是对字集说得最明了的一篇文章了. 转发自:http://tomcat-oracle.iteye.com/blog/2037160 1. 概述 本文主要包括以下几个方面:编码基本知识,java,系 ...
- git revert all changes
点击打开链接https://www.kernel.org/pub/software/scm/git/docs/git-reset.html # Revert changes to modified f ...
- 禁止选择文本和禁用右键 v1.0
var zhonghao={ //绑定事件 myAddEvent: function(obj, sEvent, fn){if(obj.attachEvent){obj.attachEvent('on' ...
- C# 翻页设计:首页,上一页,下一页,末页 ,跳转
int pageSize = 0; //每页显示行数 int nMax = 0; //总记录数 int pageCount = 0; //页数=总记录数/每页显示行数 int pageCurrent ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- STM32学习内容和计划
一.STM32学习内容(流程) 1.学习STM32开发流程 ①MDK使用.建立工程.调试等 ②库开发方法 2.学习STM32常用外设开发 ①GPIO ②中断 ③定时器 ④串口 ⑤CAN 3.学习STM ...
- 实现目标文件与源码分开的makefile测试实验
uboot提供了两种编译策略,即可以将生成的目标文件与源码混在一起,也可以将生成的目标文件与源码分开.通过对uboot Makefile的分析,笔者编写了一个简单的实现这种功能的Makfile. 顶层 ...