Java通过webservice接口获取天气信息
通过SOAP请求的方式获取天气信息并解析返回的XML文件。
参考: http://www.webxml.com.cn/WebServices/WeatherWS.asmx
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; /**
* @ClassName WeatherUtils
* @Description TODO 天气信息数据来源(http://www.webxml.com.cn/)
* 根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数:
* 调用方法如下:输入参数:theCityName = 城市中文名称(国外城市可用英文)或城市<span class="wp_keywordlink" style="margin: 0px; padding: 0px; border: 0px; background-color: transparent;"><a target=_blank href="http://www.xuebuyuan.com/" title="代码" target="_blank" style="text-decoration: none; color: rgb(1, 150, 227);">代码</a></span>(不输入默认为上海市),如:上海 或 58367,如有
* 城市名称重复请使用城市代码查询(可通过 getSupportCity 或 getSupportDataSet 获得);返回数据: 一个一维数组 String(22),共有
* 23个元素。String(0) 到 String(4):省份,城市,城市代码,城市图片名称,最后更新时间。String(5) 到 String(11):当天的 气温,
* 概况,风向和风力,天气趋势开始图片名称(以下称:图标一),天气趋势结束图片名称(以下称:图标二),现在的天气实况,天气和生活
* 指数。String(12) 到String(16):第二天的 气温,概况,风向和风力,图标一,图标二。String(17) 到 String(21):第三天的 气温,
* 概况,风向和风力,图标一,图标二。String(22) 被查询的城市或地区的介绍
* @author zyj jayzh1993@gmail.com
* @date 2013-10-14
*/
public class WeatherUtils {
/**
* 获取SOAP的请求头,并替换其中的标志符号为用户输入的城市
*
* @param city
* 用户输入的城市名称
* @return 客户将要发送给服务器的SOAP请求
*/
private static String getSoapRequest(String city) {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body> <getWeather xmlns=\"http://WebXml.com.cn/\">"
+ "<theCityCode>" + city
+ "</theCityCode> </getWeather>"
+ "</soap:Body></soap:Envelope>");
return sb.toString();
} /**
* 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流
*
* @param city
* 用户输入的城市名称
* @return 服务器端返回的输入流,供客户端读取
* @throws Exception
*/
private static InputStream getSoapInputStream(String city) throws Exception {
try {
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
URL url = new URL(
"http://www.webxml.com.cn/WebServices/WeatherWS.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true); conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction",
"http://WebXml.com.cn/getWeather"); OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close(); InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 对服务器端返回的XML进行解析
*
* @param city
* 用户输入的城市名称
* @return 字符串 用#分割
*/
public static String getWeather(String city) {
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(city);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
StringBuffer sb = new StringBuffer();
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if(n.getFirstChild().getNodeValue().equals("查询结果为空!")) {
sb = new StringBuffer("#") ;
break ;
}
sb.append(n.getFirstChild().getNodeValue() + "#");
}
is.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 测试
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String weatherInfo = getWeather("广州");
System.out.println(weatherInfo);
}
}
Java通过webservice接口获取天气信息的更多相关文章
- java解析xml实例——获取天气信息
获取xml并解析其中的数据: package getweather.xml; import java.io.IOException; import java.util.HashMap; import ...
- java获取天气信息
通过天气信息接口获取天气信息,首先要给项目导入程序所需要的包,具体需要如下几个包: json-lib-2.4.jar ezmorph-1.0.6.jar commons-beanutils-1.8.3 ...
- 内网公告牌获取天气信息解决方案(C# WebForm)
需求:内网公告牌能够正确显示未来三天的天气信息 本文关键字:C#/WebForm/Web定时任务/Ajax跨域 规划: 1.天定时读取百度接口获取天气信息并存储至Txt文档: 2.示牌开启时请求Web ...
- Kettle通过Webservice获取天气信息
Kettle通过Webservice获取天气信息 需求: 通过kettle工具,通过webservice获取天气信息,写成xml格式文件. 思路: Kettle可通过两种选择获取webservic ...
- C#调用WebService获取天气信息
概述 本文使用C#开发Winform应用程序,通过调用<WebXml/>(URL:http://www.webxml.com.cn)的WebService服务WeatherWS来获取天气预 ...
- java通过免费接口获取ip地址的服务商信息
今天分享一个免费在线的小工具的开发代码就是通过淘宝提供的接口获取服务商信息,工具地址:http://www.yzcopen.com/seo/ipadress 代码如下: public class Yz ...
- ajax无刷新获取天气信息
浏览器由于安全方面的问题,禁止ajax跨域请求其他网站的数据,但是可以再本地的服务器上获取其他服务器的信息,在通过ajax请求本地服务来实现: <?php header("conten ...
- 半吊子学习Swift--天气预报程序-获取天气信息
昨天申请的彩云天气Api开发者今天上午已审核通过  饭后运动过后就马不停蹄的来测试接口,接口是采用经纬度的方式来获取天气信息,接口地址如下 https://api.caiyunapp.com/v2/ ...
- Java调用webservice接口方法
java调用webservice接口 webservice的 发布一般都是使用WSDL(web service descriptive langu ...
随机推荐
- 2019-03-06-day012-生成器与推导式
01 昨日回顾 迭代器: 迭代器有iter方法 next方法就是迭代器 递归: 自己调用自己 明确的结束条件 递归的最大深度 官方 1000 实际测试:998/997 import sys sys.s ...
- LeetCode--122、167、169、189、217 Array(Easy)
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- KB/MB/GB。。单位换算
今天遇到一个需求,需要把数据单位进行换算,记录一下.写的不好请勿见怪. function bytesToSize( bytes ) {//单位转化 var k = 1024, ...
- 17 多校1 Add More Zero 水题
Problem Description There is a youngster known for amateur propositions concerning several mathemati ...
- pandas apply()函数参数 args
#!/usr/bin/python import pandas as pd data = {'year':[2000,2001,2002,2001,2002],'value':[1.5,1.7,3.6 ...
- 移动Web制作——JD案例
1.制作base.css 2.制作index.html 此时会考虑页面的流式布局 3.制作index.css 总结: 1.页面制作时应注意流式布局,各版本的兼容性. 2.页面的大体组成主要有:轮播图. ...
- iphone上点击div会出现半透明灰色背景以及margin失效
-webkit-tap-highlight-color 这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就会出现 ...
- Linux配置java环境变量 【随手记】
JAVA环境变量 1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序. 2. CLASSPATH环境变量.作用是 ...
- java中实现Comparable接口实现自定义排序
class Student implements Comparable{ String name; int gpa; @Override public int compareTo(Object arg ...
- Django---模版层
---https://www.cnblogs.com/liuqingzheng/articles/9509806.html 一.处理浏览器转义字符串的两种方式 1.{{ str|safe }} 2.在 ...