Android Open Source - MyWeather Utility






From Project

Back to project page MyWeather.

License

The source code is released under:

Apache License

If you think the Android project MyWeather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package util;
/*ww w  .  j a  v  a 2s.c om*/
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import model.City;
import model.County;
import model.Province;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import db.MyWeatherDB;

public class Utility {
  
  /**
   * Parser for Province
   */
  
  public synchronized static boolean handleProvincesResponse(MyWeatherDB myWeatherDB, String response){
    if (!TextUtils.isEmpty(response)){
      String[] allProvinces = response.split(",");
      if (allProvinces != null && allProvinces.length > 0){
        for (String p : allProvinces){
          String[] array = p.split("\\|");
          Province province = new Province();
          province.setProvinceCode(array[0]);
          province.setProvinceName(array[1]);
          myWeatherDB.saveProvince(province);
        }
        return true;
      }
    }
    return false;
  }
  
  /**
   * Parser for City
   */
  
  public static boolean handleCitiesResponse(MyWeatherDB myWeatherDB, String response, int provinceId){
    if(!TextUtils.isEmpty(response)){
      String[] allCities = response.split(",");
      if (allCities != null && allCities.length > 0){
        for (String c : allCities){
          String[] array = c.split("\\|");
          City city = new City();
          city.setCityCode(array[0]);
          city.setCityName(array[1]);
          city.setProvinceId(provinceId);
          myWeatherDB.saveCity(city);
        }
        return true;
      }
    }
    return false;
  }
  
  /**
   * Parser for County
   */
  
  public static boolean handleCountiesResponse(MyWeatherDB myWeatherDB, String response, int cityId){
    if(!TextUtils.isEmpty(response)){
      String[] allCounties = response.split(",");
      if (allCounties != null && allCounties.length > 0){
        for (String c : allCounties){
          String[] array = c.split("\\|");
          County county = new County();
          county.setCountyCode(array[0]);
          county.setCountyName(array[1]);
          county.setCityId(cityId);
          myWeatherDB.saveCounty(county);
        }
        return true;
      }
    }
    return false;
  }
  
  /**
   * Parser for Weather
   */
  public static void handleWeatherResponse(Context context, String response){
    try {
      JSONObject jsonObject = new JSONObject(response);
      JSONObject weatherInfo = jsonObject.getJSONObject("weatherinfo");
      String cityName = weatherInfo.getString("city");
      String weatherCode = weatherInfo.getString("cityid");
      String temp1 = weatherInfo.getString("temp1");
      String temp2 = weatherInfo.getString("temp2");
      String weatherDesp = weatherInfo.getString("weather");
      String publishTime = weatherInfo.getString("ptime");
      saveWeatherInfo(context, cityName, weatherCode, temp1, temp2, weatherDesp, publishTime);
    } catch (JSONException e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }
  
  /**
   * save weather info to sharedPreferences
   */
  public static void saveWeatherInfo(Context context, String cityName, String weatherCode, 
      String temp1, String temp2, String weatherDesp, String publishTime){
    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", cityName);
    editor.putString("weather_code", weatherCode);
    editor.putString("temp1", temp1);
    editor.putString("temp2", temp2);
    editor.putString("weather_desp", weatherDesp);
    editor.putString("publish_time", publishTime);
    editor.putString("current_date", sdf.format(new Date()));
    editor.commit();
  }
}




Java Source Code List

activity.ChooseAreaActivity.java
activity.WeatherActivity.java
db.MyWeatherDB.java
db.MyWeatherOpenHelper.java
model.City.java
model.County.java
model.Province.java
receiver.AutoUpdateReceiver.java
service.AutoUpdateService.java
util.HttpCallbackListener.java
util.HttpUtil.java
util.Utility.java