运行效果:

主要功能:

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获取天气预报的信息的更多相关文章

  1. java获取当前操作系统的信息

    java获取当前操作系统的信息 JavaOS虚拟机UnixEXT  从网上收集的一些关于java获取操作系统信息的方法,现在总结一下: 1获取本机的IP地址: private static Strin ...

  2. Java 获取到配置文件信息

    Java程序将数据库或者服务器IP写入到代码中,难免缺少灵活性. 如果写入到配置文件,部署到不通服务器上,只需要修改配置文 件即可. Java怎么读取配置文件 /** * 获取到配置文件信息 * @p ...

  3. Java 获取所有子类信息

    我以前的博客(Java Scala获取注解的类信息)介绍过通过Reflections工具通过使用特定注解的类的信息,其实本工具也可以获取接口,抽象类,类等的所有子类信息.使用方法如下: Reflect ...

  4. java获取类的信息

    关键技术剖析 1.java.lang.reflect包实现了java的反射机制,在使用反射机制时,需要导入该包. 2.Class类的forName方法能够根据类名加载类,获得类的Class对象. Cl ...

  5. Java获取电脑硬件信息

    package com.szht.gpy.util; import java.applet.Applet; import java.awt.Graphics; import java.io.Buffe ...

  6. Java获取系统相关信息System.getProperty()

    java.version Java 运行时环境版本 java.vendor Java 运行时环境供应商 java.vendor.url Java 供应商的 URL java.home Java 安装目 ...

  7. JAVA获取系统相关信息

    Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00003596 获取JRE系统相关参数 java 通过S ...

  8. java获取来访者mac信息

    根据IP获取对应的Mac地址,支持win10+Linux package com.simonjia.util.other; /** * @Author: SimonHu * @Date: 2019/6 ...

  9. Java获取 JVM 运行信息

    import java.lang.management.ClassLoadingMXBean; import java.lang.management.ManagementFactory; impor ...

随机推荐

  1. javaScripte 创建对象。。

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. easyui-helloworld

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 专题一、ArrayList增删操作技术细节详解

    一.索引检查 1)在指定位置插入元素时,第一步都需要检查输入的指定位置是否合法 public void add(int index, E element){    rangeCheckForAdd(i ...

  4. 以查询方式实现1s定时

    以查询控制器的控制位状态来实现1s定时. #include <reg52.h> sbit LED = P0^; unsigned ; void main () { LED = ; // 点 ...

  5. web2.0最全的国外API应用集合

    web2.0最全的国外API应用集合 原文地址:http://www.buguat.com/post/98.html 2.0时代,越来越多的API被大家广泛应用,如果你还不了解API是何物,请看这里的 ...

  6. ul动态增加li

    --> aaa bbb <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  7. c++读文件-对try-throw-catch的应用

    #include<iostream> #include<fstream> #include<stdlib.h> #include<stdio.h> us ...

  8. Visual Studio Code扩展

    Visual Studio Code扩展 注:本文提到的代码示例下载地址>How to create a simple extension for VS Code VS Code 是微软推出的一 ...

  9. 三菱plc编程电缆通讯端口设置方法(转载)

    三菱plc编程电缆通讯端口如何设置?三菱plc编程电缆通讯端口设置方法 时间:2015-10-21 05:09:20编辑:电工栏目:三菱plc 导读:三菱plc编程电缆通讯端口的设置方法,三菱plc上 ...

  10. bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分

    1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 665  Solved: 227[Sub ...