目录
5-1、开通阿里云市场天气预报服务
阿里云
登录
选择购买免费数据
AppCode
调用网址
发送请求
数据处理过程,涉及到:客户端应用程序,阿里云市场天气预报服务器,数据库,中国气象局数据平台
5-2、实现24小时天气预报查询功能
package com.imooc.weather;
import com.imooc.weather.impl.WeatherUtilsImpl;
import java.util.List;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
System.out.println("查询最近天气预报:");
System.out.println("输入1:查询未来24小时天气预报:");
System.out.println("输入2:查询未来3天天气预报");
System.out.println("输入3:查询未来7天天气预报");
System.out.print("请输入您的选择:");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
System.out.println("用户输入数字:" + i);
if(i==1){
System.out.print("请输入城市名称查询未来24小时天气预报:");
String city = scanner.next();
WeatherUtils weatherUtils = new WeatherUtilsImpl();
List<HourWeather> weatherList = weatherUtils.w24h("6041b3aec1454b558067ffd7148e5b29",city);
System.out.println(weatherList);
}
}
}
5-3、格式化输出天气预报结果
利用字符串格式化替代拼接字符串
package com.imooc.weather;
import java.util.List;
import java.util.Scanner;
public class Application {//创建类名
public static void main(String[] args) {//主方法
System.out.println("查询最近的天气预报: ");//输出信息
System.out.println("输入1: 查询未来24小时天气预报:");//输出信息
System.out.println("输入2: 查询未来3天天气预报");//输出信息
System.out.println("输入3: 查询未来7天天气预报");//输出信息
System.out.println("请输入您的选择");//输出信息
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
System.out.println("用户输入数字: "+1);//输出信息
if(i==1) {
System.out.print("请输入城市名称查询24小时天气预报: ");//输出信息
String city = scanner.next();String city1 = scanner.next();
WeatherUtils weatherUtils = new WeatherUtils();
List<HourWeather> weatherList = weatherUtils.w24h("50a126968c294b31917b30c417eaea1d",city1);
System.out.println(weatherList); //输出信息
if(weatherList.size() == 0) {//size获取当前列表的数量总数,如果等于0则代表服务器没有查到对应城市的天气,所以要打印输出
System.out.println("抱歉,未收录您查询的城市天气数据。");
} else {
for(HourWeather hourWeather : weatherList) {
String template = "%月%s日%时|%-3s|%-20s|%-8s|%-4s℃";
String row = String.format(template,new String[] {
hourWeather.getMonth(),
hourWeather.getDay(),
hourWeather.getHour(),
hourWeather.getWindDirection(),
hourWeather.getWindPower(),
hourWeather.getWeather(),
hourWeather.getTemperature()
});
System.out.println(row);
}
}
}
5-4、实现未来3天天气预报查询功能
5-7、实现未来7天天气预报查询
THE END!!!