登录阿里云市场
登录及使用
aliyun.com
云市场→API市场→天气预报查询
进入到界面,选择[万维易源],选择免费试用
开通成功后,进入控制台
蓝色圈所标注部分是该平台与我们的程序唯一识别链接的标识码,一定留好
之后点击[万维易源]下拉界面来到服务台位置,在这里我们可以看到可以实现的功能
点击去调试,来到网关界面
选择你想查询的城市,areaCode删除后点击发送请求 ,得到如下界面
数据处理过程
在IDEA中实现项目
新建项目:weather
JDK:1.8
操作系统:win10
进入界面后在src文件夹中创建一个包:com.imooc.weather
在该包下创建一个新类:Application
在Application中创建Main方法,用于给出备选功能菜单,接收用户输入功能
在选择菜单方面,使用Scanner方法 使用户可以自由选择查询的天气预报
接下来根据用户的选择完成相对应的功能即可
24小时天气查询功能
导入jar包(类似于windows编程的DLL动态链接库)
链接: https://pan.baidu.com/s/1RuIQzOzu4c508Suxc0718w?pwd=et6w 提取码: et6w
在weather目录下创建lib文件夹,粘贴jar包
在程序中引用:File→Project Structure→Libraries→点击+号,选择java,选择util.jar包
在WeatherUtils中可以找到三个主要方法:w24h,w3d,w7d
在当前页面选择右上角的 Choose sources 导入另一个jar包,就可以看到完整的代码和方法
接下来对WeatherUtils进行实例化
WeatherUtils weatherUtils = new WeatherUtilsImpl();
实现功能:
List<HourWeather> weatherList = weatherUtils.w24h("65fc4c1b83ba4e82838aa786fac7bdbb",city);
System.out.println(weatherList);
if(weatherList.size() == 0){
System.out.println("抱歉,没有查询到您所检索城市的天气信息!");
}else {
for(HourWeather hourWeather : weatherList){
String template = "%s月%s日%s时|%s|%s|%s|%s°C";
String row = String.format(template,new String[]{
hourWeather.getMonth(),
hourWeather.getDay(),
hourWeather.getHour(),
hourWeather.getWindDirection(),
hourWeather.getWeather(),
hourWeather.getTemperature()
});
System.out.println(row);