上一章实现了天气预报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 String getCl() {
return cl;
}
public void setCl(String cl) {
this.cl = cl;
}
public String getBeauty() {
return beauty;
}
public void setBeauty(String beauty) {
this.beauty = beauty;
}
public String getUv() {
return uv;
}
public void setUv(String ziWaiXian) {
this.uv = ziWaiXian;
}
public String getZs() {
return zs;
}
public void setZs(String zs) {
this.zs = zs;
}
public String getYh() {
return yh;
}
public void setYh(String yh) {
this.yh = yh;
}
public String getNl() {
return nl;
}
public void setNl(String nl) {
this.nl = nl;
}
}
3) WeatherBeanForecast.java
package com.example.zhaoxin.myweatherforecast.Bean;
/**
* Created by zhaoxin on 17/9/6.
* 未来七日天气
*/
public class WeatherBeanForecast {
//天气类型
private String dayWeather;
//日期
private String day;
//星期几
private String weekDay;
//晚间温度
private String nightAirTemperature;
//白天温度
private String dayAirTemperature;
//天气图片
private String dayWeatherPic;
public WeatherBeanForecast(String dayWeather, String day, String weekDay,
String nightAirTemperature, String dayAirTemperature, String dayWeatherPic) {
this.dayWeather = dayWeather;
this.day = day;
this.weekDay = weekDay;
this.nightAirTemperature = nightAirTemperature;
this.dayAirTemperature = dayAirTemperature;
this.dayWeatherPic = dayWeatherPic;
}
public String getDayWeather() {
return dayWeather;
}
public void setDayWeather(String dayWeather) {
this.dayWeather = dayWeather;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getWeekDay() {
return weekDay;
}
public void setWeekDay(String weekDay) {
this.weekDay = weekDay;
}
public String getNightAirTemperature() {
return nightAirTemperature;
}
public void setNightAirTemperature(String nightAirTemperature) {
this.nightAirTemperature = nightAirTemperature;
}
public String getDayAirTemperature() {
return dayAirTemperature;
}
public void setDayAirTemperature(String dayAirTemperature) {
this.dayAirTemperature = dayAirTemperature;
}
public String getDayWeatherPic() {
return dayWeatherPic;
}
public void setDayWeatherPic(String dayWeatherPic) {
this.dayWeatherPic = dayWeatherPic;
}
}
package com.example.zhaoxin.myweatherforecast.utils;
/**
* Created by zhaojing on 17/9/17.
* 天气加载类,继承了AsyncTaskLoader,在loadInBackground()中完成网络数据的请求
*/
public class WeatherLoader extends AsyncTaskLoader> {
private List mWeatherBeanNowList;
private List mWeatherBeanTodayList;
private List mWeatherBeanForecastList;
private List mWeatherList;
private String mCityName;
private Context mContext;
public WeatherLoader(Context context, String cityName) {
super(context);
this.mCityName = cityName;
mWeatherBeanNowList = new ArrayList<>();
mWeatherBeanTodayList = new ArrayList<>();
mWeatherBeanForecastList = new ArrayList<>();
mWeatherList = new ArrayList<>();
}
/**
* 通过该方法启动加载
*/
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
protected void onStopLoading() {
cancelLoad();
}
/**
* 在子线程中完成网络数据请求
* @return
*/
@Override
public List loadInBackground() {
final String host = "http://ali-weather.showapi.com";
final String path = "/area-to-weather";
final String method = "GET";
String appcode = "c402356e77824e7d8a23eebf6cf6d0b5";
final Map headers = new HashMap<>();
final Map querys = new HashMap<>();
headers.put("Authorization", "APPCODE " + appcode);
querys.put("area", mCityName);
querys.put("need3HourForcast", "0");
querys.put("needAlarm", "0");
querys.put("needHourData", "1");
querys.put("needIndex", "1");
querys.put("needMoreDay", "1");
String content = "";
HttpResponse response;
try {
response = HttpUtils.doGet(host, path, method, headers, querys);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
content += s;
}
Log.d("datadata_",content);
//获得全部数据(json格式)
JSONObject jsonObject = JSONObject.parseObject(content);
JSONObject showApiResBody = (JSONObject) jsonObject.get("showapi_res_body");
Log.d("data_", "all-->" + JSONObject.parseObject(content).toString());
Log.d("data_", "f1-->" + showApiResBody.get("f1"));
//解析实时数据
if (jsonObject.get("showapi_res_code").equals(0)) {
JSONObject jsonObjectNow = (JSONObject) showApiResBody.get("now");
Log.d("data_", "jsonObjectNow-->" + jsonObjectNow.toString());
String area = jsonObjectNow.getJSONObject("aqiDetail").get("area").toString();
String pm25 = jsonObjectNow.getJSONObject("aqiDetail").get("pm2_5").toString();
String quality = jsonObjectNow.getJSONObject("aqiDetail").get("quality").toString();
String temperature = jsonObjectNow.get("temperature").toString();
String temperatureTime = jsonObjectNow.get("temperature_time").toString();
String weather = jsonObjectNow.get("weather").toString();
String windDirection = jsonObjectNow.get("wind_direction").toString();
String windPower = jsonObjectNow.get("wind_power").toString();
String sd = jsonObjectNow.get("sd").toString();
WeatherBeanNow weatherBeanNow = new WeatherBeanNow(area, pm25, quality, temperature,
temperatureTime, weather, windDirection, windPower, sd);
mWeatherBeanNowList.add(weatherBeanNow);
mWeatherList.add(mWeatherBeanNowList);
} else {
Log.d("thread_", "网络连接出错,请稍后重试");
//Toast.makeText(mContext, "网络连接出错,请稍后重试", Toast.LENGTH_SHORT).show();
}
//解析今日天气
if (jsonObject.get("showapi_res_code").equals(0)) {
JSONObject jsonObjectToday = (JSONObject) showApiResBody.get("f1");
JSONObject jsonObject1 = (JSONObject) jsonObjectToday.get("index");
String clothes = jsonObject1.getJSONObject("clothes").get("desc").toString();
String cl = jsonObject1.getJSONObject("cl").get("desc").toString();
String beauty = jsonObject1.getJSONObject("beauty").get("desc").toString();
String uv = jsonObject1.getJSONObject("uv").get("desc").toString();
String zs = jsonObject1.getJSONObject("zs").get("desc").toString();
String yh = jsonObject1.getJSONObject("yh").get("desc").toString();
String nl = jsonObject1.getJSONObject("nl").get("desc").toString();
String ziWaiXian = jsonObjectToday.get("ziwaixian").toString();
WeatherBeanToday weatherBeanToday = new WeatherBeanToday(clothes, cl, beauty, uv, zs, yh, nl, ziWaiXian);
mWeatherBeanTodayList.add(weatherBeanToday);
mWeatherList.add(mWeatherBeanTodayList);
} else {
Log.d("thread_", "网络连接出错,请稍后重试");
//Toast.makeText(mContext, "网络连接出错,请稍后重试", Toast.LENGTH_SHORT).show();
}
//解析未来七天天气
if (jsonObject.get("showapi_res_code").equals(0)) {
JSONObject jsonObjectForecast1 = (JSONObject) showApiResBody.get("f1");
String dayWeather = jsonObjectForecast1.get("day_weather").toString();
String day = jsonObjectForecast1.get("day").toString();
String weekDay = jsonObjectForecast1.get("weekday").toString();
String nightAirTemperature = jsonObjectForecast1.get("night_air_temperature").toString();
String dayAirTemperature = jsonObjectForecast1.get("day_air_temperature").toString();
String dayWeatherPic = jsonObjectForecast1.get("day_weather_pic").toString();
WeatherBeanForecast weatherBeanForecast = new WeatherBeanForecast(dayWeather,
day, weekDay, nightAirTemperature, dayAirTemperature, dayWeatherPic);
mWeatherBeanForecastList.add(weatherBeanForecast);
JSONObject jsonObjectForecast2 = (JSONObject) showApiResBody.get("f2");
dayWeather = jsonObjectForecast2.get("day_weather").toString();
day = jsonObjectForecast2.get("day").toString();
weekDay = jsonObjectForecast2.get("weekday").toString();
nightAirTemperature = jsonObjectForecast2.get("night_air_temperature").toString();
dayAirTemperature = jsonObjectForecast2.get("day_air_temperature").toString();
dayWeatherPic = jsonObjectForecast2.get("day_weather_pic").toString();
weatherBeanForecast = new WeatherBeanForecast(dayWeather,
day, weekDay, nightAirTemperature, dayAirTemperature, dayWeatherPic);
mWeatherBeanForecastList.add(weatherBeanForecast);
JSONObject jsonObjectForecast3 = (JSONObject) showApiResBody.get("f3");
dayWeather = jsonObjectForecast3.get("day_weather").toString();
day = jsonObjectForecast3.get("day").toString();
weekDay = jsonObjectForecast3.get("weekday").toString();
nightAirTemperature = jsonObjectForecast3.get("night_air_temperature").toString();
dayAirTemperature = jsonObjectForecast3.get("day_air_temperature").toString();
dayWeatherPic = jsonObjectForecast3.get("day_weather_pic").toString();
weatherBeanForecast = new WeatherBeanForecast(dayWeather,
day, weekDay, nightAirTemperature, dayAirTemperature, dayWeatherPic);
mWeatherBeanForecastList.add(weatherBeanForecast);
JSONObject jsonObjectForecast4 = (JSONObject) showApiResBody.get("f4");
dayWeather = jsonObjectForecast4.get("day_weather").toString();
day = jsonObjectForecast4.get("day").toString();
weekDay = jsonObjectForecast4.get("weekday").toString();
nightAirTemperature = jsonObjectForecast4.get("night_air_temperature").toString();
dayAirTemperature = jsonObjectForecast4.get("day_air_temperature").toString();
dayWeatherPic = jsonObjectForecast4.get("day_weather_pic").toString();
weatherBeanForecast = new WeatherBeanForecast(dayWeather,
day, weekDay, nightAirTemperature, dayAirTemperature, dayWeatherPic);
mWeatherBeanForecastList.add(weatherBeanForecast);
JSONObject jsonObjectForecast5 = (JSONObject) showApiResBody.get("f5");
dayWeather = jsonObjectForecast5.get("day_weather").toString();
day = jsonObjectForecast5.get("day").toString();
weekDay = jsonObjectForecast5.get("weekday").toString();
nightAirTemperature = jsonObjectForecast5.get("night_air_temperature").toString();
dayAirTemperature = jsonObjectForecast5.get("day_air_temperature").toString();
dayWeatherPic = jsonObjectForecast5.get("day_weather_pic").toString();
weatherBeanForecast = new WeatherBeanForecast(dayWeather,
day, weekDay, nightAirTemperature, dayAirTemperature, dayWeatherPic);
mWeatherBeanForecastList.add(weatherBeanForecast);
JSONObject jsonObjectForecast6 = (JSONObject) showApiResBody.get("f6");
dayWeather = jsonObjectForecast6.get("day_weather").toString();
day = jsonObjectForecast6.get("day").toString();
weekDay = jsonObjectForecast6.get("weekday").toString();
nightAirTemperature = jsonObjectForecast6.get("night_air_temperature").toString();
dayAirTemperature = jsonObjectForecast6.get("day_air_temperature").toString();
dayWeatherPic = jsonObjectForecast6.get("day_weather_pic").toString();
weatherBeanForecast = new WeatherBeanForecast(dayWeather,
day, weekDay, nightAirTemperature, dayAirTemperature, dayWeatherPic);
mWeatherBeanForecastList.add(weatherBeanForecast);
JSONObject jsonObjectForecast7 = (JSONObject) showApiResBody.get("f7");
dayWeather = jsonObjectForecast7.get("day_weather").toString();
day = jsonObjectForecast7.get("day").toString();
weekDay = jsonObjectForecast7.get("weekday").toString();
nightAirTemperature = jsonObjectForecast7.get("night_air_temperature").toString();
dayAirTemperature = jsonObjectForecast7.get("day_air_temperature").toString();
dayWeatherPic = jsonObjectForecast7.get("day_weather_pic").toString();
weatherBeanForecast = new WeatherBeanForecast(dayWeather,
day, weekDay, nightAirTemperature, dayAirTemperature, dayWeatherPic);
mWeatherBeanForecastList.add(weatherBeanForecast);
mWeatherList.add(mWeatherBeanForecastList);
} else {
Toast.makeText(mContext, "网络连接出错,请稍后重试", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return mWeatherList;
}
}
至此,天气数据的解析就完成了。