上一章实现了天气预报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.5private 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;}
}
  1. 有了这三个类,那么我们就可以开始解析了,加上上一篇写的获取数据的部分,代码如下:
package com.example.zhaoxin.myweatherforecast.utils;
/*** Created by zhaojing on 17/9/17.* 天气加载类,继承了AsyncTaskLoader,在loadInBackground()中完成网络数据的请求*/
public class WeatherLoader extends AsyncTaskLoader<List<List>> {private List<WeatherBeanNow> mWeatherBeanNowList;private List<WeatherBeanToday> mWeatherBeanTodayList;private List<WeatherBeanForecast> mWeatherBeanForecastList;private List<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<>();}/*** 通过该方法启动加载*/@Overrideprotected void onStartLoading() {forceLoad();}@Overrideprotected void onStopLoading() {cancelLoad();}/*** 在子线程中完成网络数据请求* @return*/@Overridepublic List<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<String, String> headers = new HashMap<>();final Map<String, String> 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;}
}

至此,天气数据的解析就完成了。

Android 端天气预报APP的实现(三)JSON解析天气数据相关推荐

  1. react native Android端保持APP后台运行--封装 Headless JS

    react native Android端保持APP后台运行--封装 Headless JS 前些日子在做后台下载时踩了后台运行这个大坑,RN官网文档上面在安卓上提供了Headless JS方法,iO ...

  2. 搭建直播平台过程中Android端直播APP源码是如何实现连麦功能的?

    直播平台强大的变现能力是大家有目共睹的,很多开发商在搭建直播平台时为了增加用户黏性,纷纷将直播中加入连麦功能. 目前市场上通用的有两种连麦方案:本地混流和云端混流.本地混流即主播和连麦观众分别推一路流 ...

  3. 爬虫爬取京东商品详细数据 (品牌、售价、各类评论量(精确数量)、热评词及数量等)json解析部分数据

    文章目录 前言 一.数据保存格式设置及数据库准备(CentOS云mysql数据库) 1.分析数据需求(单一商品为例) 2.数据库保存格式 3.用到的数据库操作及指令 二.网页分析 1.分析网页源码,确 ...

  4. json解析出来数据为空解决方法

    json解析出来数据为空解决方法 参考文章: (1)json解析出来数据为空解决方法 (2)https://www.cnblogs.com/yifan72/p/8900825.html (3)http ...

  5. Android简易天气预报App

    先看下app效果图: App介绍:首次启动应用时列表显示全国34个省份及直辖市包括港澳台,如果选择省份进入所在省份下的市列表,如果再选择市项进入该市下所有的区或县(包括该市)列表,如果再选择该列表下的 ...

  6. 用于android天气开发的背景图,Android开发天气预报APP的设计与实现毕业设计.pdf

    摘要 随着移动互联网技术和通信技术的发展,智能手机几乎成为人们 生活的必需品.近年来,Android系统已经成为智能手机中用户量最 多的操作系统.通过Android程序开发和设计天气预报手机应用,可 ...

  7. android实现天气预报App(0)

    首先 来看看目录结构 activity:主要是天气预报的各个页面 db:存放省.市.县三种数据的实体类 gson:存放获取的天气相关的json实体类 service:天气等数据更新相关代码的存储 ut ...

  8. 简单的Android端新闻App的实现。

    1. 更新记录: 2021/11/14: 1.更新了数据来源的 api 使用了聚合数据的 新闻 api 2.使用了 TabLayout 代替原来的 textview 组. 2021/11/13: 1. ...

  9. 简单的Android端新闻App的实现

    先上效果图: 图一  :     图二:    总体思路概述:        如图本app界面简单,图一的最顶端是安卓原生的标题栏,图二的最顶端是我自己定义的标题栏,具体代码后面再说.图一标题栏下面是 ...

最新文章

  1. 最长上升子序列(LIS)长度及其数量
  2. Activiti 6.x【11】IntermediateEvent
  3. Python JS Jquery Json 转换关系
  4. jupyter notebook 进阶使用:nbextensions
  5. linux基础命令篇一
  6. 搭建MySQL+MHA服务易错位置
  7. css:before和after中的content属性
  8. php 时间戳存储 原因,将php文件中的unix时间戳存储到mysql中(store unix timestamp from php file into mysql)...
  9. MSI/MSI-X Capability结构 (转)
  10. 信息化与工业化融合的内涵、层次和方向
  11. MySQL教程(十一)—— 操作数据表中的记录
  12. 路由器setup模式
  13. 1.PHP7内核剖析 --- PHP 基础架构
  14. 微型计算机控制技术 高国琴,微型计算机控制技术论文集 微型计算机控制技术外文文献怎么找...
  15. mysql版本查询命令
  16. Quartus II :1位全加器设计
  17. 【X86】---关于Intel芯片架构的发展史
  18. java中==与equals的区别
  19. 异步四位二进制计数器逻辑图
  20. ElasticSearch语法整理-DSL语言高级查询

热门文章

  1. Linux中make工具及makefile文件
  2. 华为机试:根据员工出勤信息,判断本次是否能获得出勤奖
  3. 二维码生成免费API
  4. 互联网寒冬下,在校生如何寻找自己的定位?
  5. 跳转指令和循环指令详解
  6. h5短信匿名信源码一封来信你的Ta的一封来信表白祝福道告白信
  7. tranform(2D)改变盒子的形态(位移,旋转,缩放)
  8. Camx架构开UMD、KMD log以及dump图的方式
  9. 深度学习-三维卷积神经网络(3DCNN)
  10. kafka:Failed to add leader for partitions(暴力解决)