解析处理服务器返回的JSON数据
public static void handleWeatherResponse(Context context, String response){
try{
JSONObject jsonObject = new JSONObject(response);
JSONObject weatherInfo = jsonObject.getJSONObject("weatherInfo");
String cityName = weatherInfo.getString("cityname");
String weatherCode = weatherCode.getString("cityid");
......
saveWeatherInfo(cityName,);
}catch(JSONException){
e.printStackTrace();
}
}
/**
*将服务器返回的所有信息存储到ShaerdPreferences文件中
**/
public static void saveWeatherInfo(Context context, String cityName, String...){
SimpleDateFormat saf = new SimpleDateFormat("yyyy年M月d日", Locale.CHINA);
ShaerdPreferences.Editor editor = new PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putBoolean("city_selected", true);
editor.putString("current_date", sdf.format(new Date()));
editor.commit();
}
/**
*从SharedPreference中读取存储的天气信息
**/
private void showWeather(){
SharedPreferences perfs = PreferenceManager.getDefaultSharedPreferences(this);
cityNameText.setText(perfs.getString("city_name", "" ));
}