package com.example.test.coolweather.utility;

import android.text.TextUtils;

import com.example.test.coolweather.db.CoolWeatherDB;
import com.example.test.coolweather.model.City;
import com.example.test.coolweather.model.County;
import com.example.test.coolweather.model.Province;

/**
 * Created by test on 8/12/2015.
 */
public class Utility {
    public synchronized static boolean handleProvinceResponse
            (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.setProvince_code(array[0]);
                    province.setProvince_name(array[1]);
                    coolWeatherDB.saveProvince(province);
                }
                return true;
            }
        }
        return false;
    }

    public synchronized static boolean handleCityResponse
            (CoolWeatherDB coolWeatherDB, String response, int province_id) {
        if (!TextUtils.isEmpty(response)) {
            String[] allCitys = response.split(",");
            if (allCitys != null && allCitys.length > 0) {
                for (String c : allCitys) {
                    City city = new City();
                    String[] array = c.split("\\|");
                    city.setCity_code(array[0]);
                    city.setCity_name(array[1]);
                    city.setProvince_id(province_id);
                    coolWeatherDB.saveCity(city);
                }
                return true;
            }
        }
        return false;
    }

    public synchronized static boolean handleCountyResponse
            (CoolWeatherDB coolWeatherDB, String response, int city_id) {
        if (!TextUtils.isEmpty(response)) {
            String[] allCountys = response.split(",");
            if (allCountys != null && allCountys.length > 0) {
                for (String co : allCountys) {
                    County county = new County();
                    String[] array = co.split("\\|");
                    county.setCounty_code(array[0]);
                    county.setCounty_name(array[1]);
                    county.setCity_id(city_id);
                    coolWeatherDB.saveCounty(county);
                }
                return true;
            }
        }
        return false;
    }
}

@@ -40,16 +40,135 @@
 ility;%0A%0A
+import android.content.Context;%0Aimport android.content.SharedPreferences;%0Aimport android.preference.PreferenceManager;%0A
 import a
@@ -400,71 +400,1906 @@
 e;%0A%0A
-/**%0A * Created by test on 8/12/2015.%0A */%0Apublic class Utility %7B
+import org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date;%0Aimport java.util.Locale;%0A%0A/**%0A * Created by test on 8/12/2015.%0A */%0Apublic class Utility %7B%0A    public static void handleWeatherResponse(Context context, String response) %7B%0A        try %7B%0A            JSONObject jsonObject = new JSONObject(response);%0A            JSONObject weatherInfo = jsonObject.getJSONObject(%22weatherinfo%22);%0A            String city_name = weatherInfo.getString(%22city%22);%0A            String weather_code = weatherInfo.getString(%22cityid%22);%0A            String high_temp = weatherInfo.getString(%22temp1%22);%0A            String low_temp = weatherInfo.getString(%22temp2%22);%0A            String weather_des = weatherInfo.getString(%22weather%22);%0A            String public_time = weatherInfo.getString(%22ptime%22);%0A            saveWeatherInfo(context,city_name,weather_code,%0A                    high_temp,low_temp,weather_des,public_time);%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    private static void saveWeatherInfo%0A            (Context context, String city_name, String weather_code,%0A             String high_temp, String low_temp, String weather_des, String public_time) %7B%0A        SimpleDateFormat sdf = new SimpleDateFormat(%22yyyy%E5%B9%B4m%E6%9C%88d%E6%97%A5%22, Locale.CHINA);%0A        SharedPreferences.Editor editor =%0A                PreferenceManager.getDefaultSharedPreferences(context).edit();%0A        editor.putBoolean(%22city_seleted%22, true);%0A        editor.putString(%22city_name%22, city_name);%0A        editor.putString(%22weather_code%22, weather_code);%0A        editor.putString(%22high_temp%22, high_temp);%0A        editor.putString(%22low_temp%22, low_temp);%0A        editor.putString(%22weather_des%22, weather_des);%0A        editor.putString(%22public_time%22, public_time);%0A        editor.putString(%22current_data%22, sdf.format(new Date()));%0A        editor.commit();%0A    %7D%0A
 %0A   

create a json parser to handle weather response