/** 使用Gson解析服务器返回的JSON数据,并将解析的数据存储到本地 */ public static void handleWeatherResponse(Context context, String response) { try { LogUtil.i("UTILITY", "----------------->" + response.toString()); Gson gson = new Gson(); Weather weather = gson.fromJson(response, Weather.class); LogUtil.i("UTILITY", "----------------->" + weather.toString()); Weatherinfo info = weather.getWeatherinfo(); LogUtil.i("UTILITY", "----------------->" + info.toString()); saveWeatherInfo(context, info); } catch (Exception e) { // TODO: handle exception } }
/** 将服务器返回的天气信息存储到共享参数中 */ public static void saveWeatherInfo(Context context, Weatherinfo info) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日", Locale.CHINA); SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); editor.putBoolean("city_selected", true); editor.putString("city_name", info.getCity()); editor.putString("weather_code", info.getCityid()); editor.putString("temp1", info.getTemp1()); editor.putString("temp2", info.getTemp2()); editor.putString("weather_desp", info.getWeather()); editor.putString("publish_time", info.getPtime()); LogUtil.i("UTILITY", "----------------->" + sdf.format(new Date())); editor.putString("current_date", sdf.format(new Date())); editor.commit(); }