-----------------------------------该文章代码已停更,可参考浩比天气(更新于2019/6/25)-----------------------------------
接上篇文章(http://blog.csdn.net/MaybeForever/article/details/78497806)继续,完整文件请从我的GitHub中下载。
目录(下)
文章目录
##四、显示天气信息
1、定义GSON实体类
首先详细分析一下和风天气返回的数据,如下图所示:
除了status之外,其余的五个内部又包含具体的内容,因此将basic、aqi、now、suggestion、daily_forecast这五个部分定义成5个实体类。在gson包下建立Basic类、AQI类、Now类、Suggestion类、Forecast类。
Basic类:
public class Basic {
@SerializedName("city")
public String cityName;//城市名
@SerializedName("id")
public String weatherId;//城市对应的天气的id
@SerializedName("lat")
public String cityLat;//城市的经度
@SerializedName("lon")
public String cityLon;//城市的纬度
public Update update;
public class Update{
@SerializedName("loc")
public String updateTime;//接口更新时间
}
}
AQI类:
public class AQI {
public AQICITY city;
public class AQICITY{
public String aqi;//空气质量指数
public String co;//一氧化碳指数
public String no2;//二氧化氮指数
public String o3;//臭氧指数
public String pm10;//PM10指数
public String pm25;//PM2.5指数
public String qlty;//空气质量(优/良/轻度污染/中度污染/重度污染/严重污染)
public String so2;//二氧化硫指数
}
}
Forecast类:
public class Forecast {
public Astro astro;
public class Astro{
public String mr;//月升时间
public String ms;//月落时间
public String sr;//日升时间
public String ss;//日落时间
}
@SerializedName("cond")
public More more;
public class More{
@SerializedName("txt_d")
public String info;//白天天气状况描述
@SerializedName("txt_n")
public String night_info;//晚间天气状况描述
}
public String date;//预报日期
public String pcpn;//降水量
public String pop;//降水概率
public String pres;//大气压强
public String uv;//紫外线强度指数
public String vis;//能见度
public String hum;//相对湿度
@SerializedName("tmp")
public Temperature temperature;
public class Temperature{
public String max;//最高温度
public String min;//最低温度
}
public Wind wind;
public class Wind{
public String dir;//风向
public String sc;//风力
public String spd;//风速
}
}
Now类:
public class Now {
@SerializedName("cond")
public More more;
public class More{
@SerializedName("txt")
public String info;//天气信息
}
public String fl;//体感温度
public String hum;//相对湿度
public String pcpn;//降水量
public String pres;//大气压强
@SerializedName("tmp")
public String temperature;//温度
public String vis;//能见度
public WIND wind;
public class WIND{
public String dir;//风向
public String sc;//风力
public String spd;//风速
}
}
Suggestion类:
public Air air;//空气质量指数
public class Air{
@SerializedName("txt")
public String info;
}
@SerializedName("comf")
public Comfort comfort;//舒适度指数
public class Comfort{
@SerializedName("txt")
public String info;
}
@SerializedName("cw")
public CarWash carWash;//洗车指数
public class CarWash{
@SerializedName("txt")
public String info;
}
public Drsg drsg;//穿衣指数
public class Drsg{
@SerializedName("txt")
public String info;
}
public Flu flu;//感冒指数
public class Flu{
@SerializedName("txt")
public String info;
}
public Sport sport;//运动指数
public class Sport{
@SerializedName("txt")
public String info;
}
public Trav trav;//旅游指数
public class Trav{
@SerializedName("txt")
public String info;
}
public Uv uv;//紫外线指数
public class Uv{
@SerializedName("txt")
public String info;
}
}
在创建五个实体类的过程中,我们使用了@SerializedName() 来命名JSON中的一些字段,由于JSON中的一些字段不适合直接用来使用,因为不好理解,所以可以使用@SerializedName()的方式 ,将JSON字段写在里面,然后在下面一行写上自己需要用的命名(可随意写,只要自己理解就可以)。至此,basic、aqi、now、suggestion、daily_forecast对应的实体类已经创建好,接下来还需要再创建一个总的实体类来引用刚刚创建的各个实体类。在gson包下新建一个Weather类,代码如下:
public class Weather {
/**
* Weather类作为总的实例类来引用以上各个实体类
*/
public String status;
public Basic basic;
public AQI aqi;
public Now now;
public Suggestion suggestion;
@SerializedName("daily_forecast")
public List<Forecast> forecastList;
}
在Weather类中,对Basic、AQI、Now、Suggestion、Forecast类进行了引用,由于daily_forecast包含的是一个数组,因此使用List集合来引用Forecast类。此外,除了天气信息数据还会包含一项status数据,成功获取天气数据的时候会返回ok,失败时会返回具体原因。
2、编写天气界面
首先创建一个用于显示天气信息的活动。右击com.coolweather.android包->New->Activity->Empty Activity,创建一个Weather Activity,并将布局名指定成activity_weather.xml。由于所有的天气信息在同一界面上显示会让代码很混乱,所以将界面的不同部分写在不同的布局文件里,再通过引入布局的方式集成到activity_weather.xml中。右击res/layout->New->Layout resource file,新建一个title.xml作为头布局,代码如下所示:
<android.support.percent.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp">
<Button
android:id="@+id/nav_button"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="left|top"
app:layout_widthPercent="20%"
app:layout_heightPercent="80%"
android:background="@drawable/ic_home"/>
<TextView
android:id="@+id/title_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:gravity="center"
app:layout_widthPercent="60%"
app:layout_heightPercent="80%"
android:textColor="#fff"
android:textSize="40sp"/>
<TextView
android:id="@+id/title_update_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_gravity="right|top"
app:layout_widthPercent="20%"
app:layout_heightPercent="80%"
android:textColor="#fff"
android:textSize="16sp"/>
<TextView
android:id="@+id/lat_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_gravity="left|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="20%"
android:textColor="#fff"
android:textSize="16sp"/>
<TextView
android:id="@+id/lon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="20%"
android:textColor="#fff"
android:textSize="16sp"/>
</android.support.percent.PercentFrameLayout>
title.xml使用了百分比布局的方法,然后新建now.xml作为当前天气信息的布局,代码如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#8000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="实时天气信息"
android:textColor="#fff"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:id="@+id/degree_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="40sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="温度"
android:textColor="#fff" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:id="@+id/fl_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="40sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="体感温度"
android:textColor="#fff" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="@+id/weather_info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="40sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_