不管是工作中还是生活中,我们都需要用到天气预报,作为一个码农,天气的需求也是被挂在嘴边的。最近公司的客户提出要获取设备安装地的实时天气,本猿就研究了下中央气象局的天气接口(一般比较常用)。
废话少说,先附上中央气象局的接口链接:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
这是接口的文档信息,里面有多个接口可供选择,本猿只使用了其中的一个,其实原理都是一样的。
首先,打开eclipse,新建一个java project,创建之后,选中src目录,右键新建 Web Service Client
输入刚才的链接,当然需要在最后加上?wsdl,http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
链接也已给出,直接finish。
就可以看到如下的package
到此,项目加载webservice就算完成了,接下来就是代码实现了,具体的步骤,注意事项都已写在code里了,本猿在这里就不多解释了,直接上代码。
package com.dlbd.test; import java.rmi.RemoteException; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.rpc.ServiceException; import cn.com.WebXml.WeatherWebServiceLocator; import cn.com.WebXml.WeatherWebServiceSoapStub; /** * 中央气象局天气预报接口 * 该接口免费用户(同一IP)24小时只能访问70次左右 * 不能访问频率过快(必须加延时sleep,目前只测试了间隔一秒),不然会无法正常显示 * * @author amao * */ public class TestWeather { public static String getWeatherByCity(String city) { //拼接天气 String str = null; try { WeatherWebServiceLocator locator = new WeatherWebServiceLocator(); // 调用xxLocator对象的getXXPort()方法生成xxSoapBindingStub对象 WeatherWebServiceSoapStub stud = (WeatherWebServiceSoapStub) locator.getWeatherWebServiceSoap(); // 输出当天的天气状况 String[] weather = stud.getWeatherbyCityName(city); // 获取当前时间 SimpleDateFormat sdf = new SimpleDateFormat("y年M月d日"); Date nowDate = new Date(); String time = sdf.format(nowDate); // TODO 这边可以根据自己的业务去选择性的编写 /** * 这是本人所在公司需要获取当天的气象,请求时间不同可能获取的字符串就不同 所以在此进行了三次判断,保证获取的是当天的天气 */ if (weather[6].contains(time.substring(time.indexOf("年") + 1))) { // 截取天气状况 String weatherStatus = weather[6].substring(weather[6].indexOf(" ") + 1); // 拼接天气 str = weather[1] + ":" + weatherStatus + "\n" + weather[5] + " " + weather[7]; } else if (weather[13].contains(time.substring(time.indexOf("年") + 1))) { // 截取天气状况 String weatherStatus = weather[13].substring(weather[13].indexOf(" ") + 1); // 拼接天气 str = weather[1] + ":" + weatherStatus + "\n" + weather[12] + " " + weather[14]; } else if (weather[18].contains(time.substring(time.indexOf("年") + 1))) { // 截取天气状况 String weatherStatus = weather[18].substring(weather[18].indexOf(" ") + 1); // 拼接天气 str = weather[1] + ":" + weatherStatus + "\n" + weather[17] + " " + weather[19]; } } catch (Exception e) { e.printStackTrace(); } return str; } public static void main(String[] args) throws RemoteException, ServiceException { String weather = getWeatherByCity("大连"); System.out.println(weather); } }下面附上源码路径 csdn:https://download.csdn.net/download/qq_35062384/10711313
github:https://github.com/amaohaoshuai/weather