JSON格式的天气信息解析并储到本地SharedPreferences
2016-06-06 09:39
330 查看
将JSON格式的天气信息进行解析,并储到本地SharedPreferences,以下为简单源代码:
/** * 解析服务器返回的JSON数据,并将解析出的数据存储到本地。 * @param context * @param response 网络返回数据字符串 */ public static void handleWeatherResponse(Context context, String response){ try { JSONObject jsonObject=new JSONObject(response); JSONObject weatherInfo=jsonObject.getJSONObject("result"); String date=weatherInfo.getString("days"); //当前日期 String week=weatherInfo.getString("week"); //星期 String cityName=weatherInfo.getString("citynm"); //城市名 String temperature_curr=weatherInfo.getString("temperature_curr"); //当前温度 String humidity=weatherInfo.getString("humidity"); //湿度 String weather=weatherInfo.getString("weather"); //天气情况 String wind=weatherInfo.getString("wind"); //风向 String winp=weatherInfo.getString("winp"); //风级 String temp_high = weatherInfo.getString("temp_high"); String temp_low = weatherInfo.getString("temp_low"); saveWeatherInfo(context, date, week,cityName,temperature_curr, humidity, weather, wind,winp,temp_high,temp_low); } catch (JSONException e) { e.printStackTrace(); } } /** * 将服务器返回的所有天气信息存储到SharedPreferences。 * @param context * @param date 日期 * @param week 星期 * @param cityName 城市名 * @param temperature_curr 当前温度 * @param humidity 湿度 * @param weather 天气情况 * @param wind 风向 * @param winp 风力 * @param temp_high 最高温度 * @param temp_low 最低温度 */ private static void saveWeatherInfo(Context context, String date, String week, String cityName, String temperature_curr, String humidity, String weather, String wind, String winp, String temp_high, String temp_low) { SharedPreferences.Editor editor= PreferenceManager.getDefaultSharedPreferences(context).edit(); editor.putBoolean("city_selected", true); editor.putString("days",date); editor.putString("week",week); editor.putString("city_name", cityName); editor.putString("temperature_curr", temperature_curr); editor.putString("humidity", humidity); editor.putString("weather", weather); editor.putString("wind", wind); editor.putString("winp", winp); editor.putString("temp_high", temp_high); editor.putString("temp_low", temp_low); editor.commit(); }
相关文章推荐
- 我是运营,我没有假期
- 使用 Python 处理 JSON 格式的数据
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- DB2数据库的安装
- C#实现把指定数据写入串口
- “传奇”图象数据存储方式
- newtonsoft.json解析天气数据出错解决方法
- 修复mysql数据库
- 浅析SQL数据操作语句
- SQLServer 数据导入导出的几种方法小结
- 简述MySQL分片中快速数据迁移
- MySQL数据备份之mysqldump的使用详解
- C#实现窗体间传递数据实例
- vbs 解析json jsonp的方法
- Ruby和Ruby on Rails中解析JSON格式数据的实例教程
- C#中的委托数据类型简介
- SQL Server删除表及删除表中数据的方法
- SqlServer2008误操作数据(delete或者update)后恢复数据的方法
- 给你的数据库文件减肥