上一章实现了天气预报API的获取,那么,今天就来讲讲获取后的数据如何解析吧~
相比大家可以看到,解析出来的数据是json格式的,那么我们需要使用json来解析天气数据。具体实现如下:
1. 首先获取的天气数据有实时天气、今日天气和未来天气,所以我先建了三个天气类,分别为WeatherBeanNow,WeatherBeanToday,WeatherBeanForecast:
1) WeatherBeanNow.java
package com.example.zhaoxin.myweatherforecast.Bean;
/**
* Created by zhaoxin on 17/9/16.
* 实时天气
*/
public class WeatherBeanNow {
//地区
private String mArea;
//pm2.5
private String mPm25;
//天气质量状况
private String mQuality;
//实时温度
private String mTemperature;
//实时时间
private String mTemperatureTime;
//天气类型
private String mWeather;
//风向
private String mWindDirection;
//风力
private String mWindPower;
//湿度
private String mSd;
public WeatherBeanNow(String mArea, String mPm25, String mQuality, String mTemperature,
String mTemperatureTime, String mWeather,
String mWindDirection, String mWindPower, String mSd) {
this.mArea = mArea;
this.mPm25 = mPm25;
this.mQuality = mQuality;
this.mTemperature = mTemperature;
this.mTemperatureTime = mTemperatureTime;
this.mWeather = mWeather;
this.mWindDirection = mWindDirection;
this.mWindPower = mWindPower;
this.mSd = mSd;
}
public String getmArea() {
return mArea;
}
public void setmArea(String mArea) {
this.mArea = mArea;
}
public String getmTemperature() {
return mTemperature;
}
public void setmTemperature(String mTemperature) {
this.mTemperature = mTemperature;
}
public String getmWeather() {
return mWeather;
}
public void setmWeather(String mWeather) {
this.mWeather = mWeather;
}
public String getmQuality() {
return mQuality;
}
public void setmQuality(String mQuality) {
this.mQuality = mQuality;
}
public String getmSd() {
return mSd;
}
public void setmSd(String mSd) {
this.mSd = mSd;
}
public String getmPm25() {
return mPm25;
}
public void setmPm25(String mPm25) {
this.mPm25 = mPm25;
}
public String getmWindDirection() {
return mWindDirection;
}
public void setmWindDirection(String mWindDirection) {
this.mWindDirection = mWindDirection;
}
public String getmWindPower() {
return mWindPower;
}
public void setmWindPower(String mWindPower) {
this.mWindPower = mWindPower;
}
public String getmTemperatureTime() {
return mTemperatureTime;
}
public void setmTemperatureTime(String mTemperatureTime) {
this.mTemperatureTime = mTemperatureTime;
}
}
2) WeatherBeanToday.java
package com.example.zhaoxin.myweatherforecast.Bean;
/**
* Created by zhaoxin on 17/9/5.
*/
public class WeatherBeanToday {
//穿衣
private String clothes;
//晨练指数
private String cl;
//化妆指数
private String beauty;
//紫外线指数
private String uv;
//中暑指数
private String zs;
//约会指数
private String yh;
//夜生活指数
private String nl;
//紫外线
private String ziWaiXian;
public String getZiWaiXian() {
return ziWaiXian;
}
public void setZiWaiXian(String ziWaiXian) {
this.ziWaiXian = ziWaiXian;
}
public WeatherBeanToday(String clothes, String cl, String beauty, String uv, String zs, String yh, String nl, String ziWaiXian) {
this.clothes = clothes;
this.cl = cl;
this.beauty = beauty;
this.uv = uv;
this.zs = zs;
this.yh = yh;
this.nl = nl;
this.ziWaiXian = ziWaiXian;
}
public String getClothes() {
return clothes;
}
public void setClothes(String clothes) {
this.clothes = clothes;
}
public Stri