Android Open Source - firstcodeandroid Utility






From Project

Back to project page firstcodeandroid.

License

The source code is released under:

MIT License

If you think the Android project firstcodeandroid 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 com.vjia.coolweather.util;
/*  www. j av a 2s  .  c  o m*/
import java.text.SimpleDateFormat;
import java.util.Locale;

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

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.vjia.coolweather.db.CoolWeatherDB;
import com.vjia.coolweather.model.City;
import com.vjia.coolweather.model.County;
import com.vjia.coolweather.model.Province;

public class Utility {

  /**
   * the response sample: code|name,code|name
   * @param coolWeatherDB
   * @param response
   * @return
   */
  public synchronized static boolean handleProvincesResponse(CoolWeatherDB coolWeatherDB, 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]);
          coolWeatherDB.saveProvince(province);
        }
        return true;
      }
    }
    return false;
  }

  public static boolean handleCitiesResponse(CoolWeatherDB coolWeatherDB, String response, int provinceId){
    if(!TextUtils.isEmpty(response)){
      String[] allCities=response.split(",");
      if(allCities!=null && allCities.length>0){
        for(String p : allCities){
          String[] array = p.split("\\|");
          City city=new City();
          city.setCityCode(array[0]);
          city.setCityName(array[1]);
          city.setProvinceId(provinceId);
          coolWeatherDB.saveCity(city);
        }
        return true;
      }
    }
    return false;
  }
  public static boolean handleCountriesResponse(CoolWeatherDB coolWeatherDB, String response, int cityId){
    if(!TextUtils.isEmpty(response)){
      String[] allCounties=response.split(",");
      if(allCounties!=null && allCounties.length>0){
        for(String p : allCounties){
          String[] array = p.split("\\|");
          City city=new City();
          County county=new County();
          county.setCountyCode(array[0]);
          county.setCountyName(array[1]);
          county.setCityId(cityId);
          coolWeatherDB.saveCounty(county);
        }
        return true;
      }
    }
    return false;
  }
  
  /**
   * ???????????????JSON?????????????????????????????
   * @param context
   * @param response
   */
  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){
      e.printStackTrace();
    }
  }

  /**
   * ????????????????????????????SharedPreferences?????
   * @param context
   * @param cityName
   * @param weatherCode
   * @param temp1
   * @param temp2
   * @param weatherDesp
   * @param publishTime
   */
  private static void saveWeatherInfo(Context context, String cityName,
      String weatherCode, String temp1, String temp2, String weatherDesp,
      String publishTime) {
    // TODO Auto-generated method stub
    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("current_date", sdf.format(new java.util.Date()));
    editor.commit();
  }
}




Java Source Code List

com.example.activitylifecycletest.DialogActivity.java
com.example.activitylifecycletest.MainActivity.java
com.example.activitylifecycletest.NormalActivity.java
com.example.activitytest.FirstActivity.java
com.example.listviewtest.FruitAdapter.java
com.example.listviewtest.Fruit.java
com.example.listviewtest.MainActivity.java
com.jikexueyuan.counttime.MainActivity.java
com.jikexueyuan.getmyphonenumber.GetNumber.java
com.jikexueyuan.getmyphonenumber.MainActivity.java
com.jikexueyuan.getmyphonenumber.MyAdapter.java
com.jikexueyuan.getmyphonenumber.PhoneInfo.java
com.vjia.bookcollector.MainActivity.java
com.vjia.coolweather.MainActivity.java
com.vjia.coolweather.activity.ChooseAreaActivity.java
com.vjia.coolweather.activity.WeatherActivity.java
com.vjia.coolweather.db.CoolWeatherDB.java
com.vjia.coolweather.db.CoolWeatherOpenHelper.java
com.vjia.coolweather.model.City.java
com.vjia.coolweather.model.County.java
com.vjia.coolweather.model.Province.java
com.vjia.coolweather.util.HttpCallbackListener.java
com.vjia.coolweather.util.HttpUtil.java
com.vjia.coolweather.util.Utility.java
com.vjia.helloandroid.FirstActivity.java
com.vjia.helloandroid.HelloAndroidActivity.java
com.vjia.hellonote.AddContent.java
com.vjia.hellonote.MainActivity.java
com.vjia.hellonote.MyAdapter.java
com.vjia.hellonote.NotesDB.java
com.vjia.hellonote.SelectAct.java
com.vjia.jokeking.GetJoke.java
com.vjia.jokeking.HttpCallbackListener.java
com.vjia.jokeking.HttpUtil.java
com.vjia.jokeking.Joke.java
com.vjia.jokeking.MainActivity.java
com.vjia.jokeking.MyAdapter.java
com.vjia.locationtest.MainActivity.java