JSON格式的天气信息解析并储到本地SharedPreferences
来源:互联网 发布:win10软件文件夹 编辑:程序博客网 时间:2024/04/28 20:28
将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(); }
0 0
- JSON格式的天气信息解析并储到本地SharedPreferences
- 联网获取天气信息并保存到本地数据库
- 整理获取天气信息的各种API和JSON解析
- SharedPreferences保存二维码扫描的网址信息到本地
- android中国天气网api获取天气信息并解析
- json_ajax提交json格式数据到servlet并解析
- 读取yahoo的天气,并下载其中的图片和FLASH到本地
- 将json格式的数据保存到本地
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- Swift学习6——解析json天气信息Demo
- 解析中国天气网的json代码:
- js 解析java后端的String为json格式并格式化Json格式的时间
- JSON格式的解析
- 解析天气信息
- 实现读取本地json格式文件并解析
- js解析php格式的表单到json对象
- 编程基础——正则表达式
- 奇葩问题,签名打包后的程序按HOME键异常退出
- 【Halcon】 模板匹配 create_template 与 fast_match_mg
- Linux常用命令:vmstat命令
- 夜间模式问题
- JSON格式的天气信息解析并储到本地SharedPreferences
- Linux升级内核
- 获取时间
- 《定风波》--苏轼之我最喜欢的一首词
- oracle11g归档日志备份
- SqlServer分组查询,row_number,partition运用
- week——input新属性,显示年周
- JS(Coursera):More On Arrays
- 九章算术卷第六 均输