app/src/main/java/net/sky/scweather/sync/WeatherSyncAdapter.java
private ContentValues[] parseWeatherDataFromJson(String jsonStr, Integer cityCode) throws JSONException {
        if (jsonStr == null) {
            return null;
        }
        Log.i(TAG, jsonStr);

        final String K_ICON = "iconPrev";
        final String K_DAY_PERIOD = "periodoDia";
        final String K_RAIN = "mmChuva";
        final String K_DESCRIPTION = "nmCondicao";
        final String K_FORECAST_DATE = "dtPrev";
        final String K_LAST_UPDATE = "dtAtualizacao";
        final String K_RELATIVE_HUMIDITY = "umidadeRel";
        final String K_WIND_DIRECTION_START = "dirVentoIni";
        final String K_WIND_DIRECTION_END = "dirVentoFin";
        final String K_WIND_SPEED_MAX = "velVentoMax";
        final String K_WIND_SPEED_AVG = "velVentoMed";
        final String K_TEMPERATURE_MAX = "tempMax";
        final String K_TEMPERATURE_MIN = "tempMin";

        JSONArray weatherArray = new JSONArray(jsonStr);
        ArrayList<ContentValues> cvsList = new ArrayList<>(weatherArray.length());

        for (int i = 0; i < weatherArray.length(); ++i) {
            JSONObject forecast = weatherArray.getJSONObject(i);
            final int icon = forecast.getInt(K_ICON);
            if (icon == 0) continue; // data unavailable, skip record
            final int dayPeriod = forecast.getInt(K_DAY_PERIOD);
            final int rain = forecast.getInt(K_RAIN);
            final String description = forecast.getString(K_DESCRIPTION);
            final long forecastDateMillis = forecast.getLong(K_FORECAST_DATE);
            final long lastUpdate = forecast.getLong(K_LAST_UPDATE);
            final int humidity = forecast.getInt(K_RELATIVE_HUMIDITY);
            final String windDirectionStart = forecast.getString(K_WIND_DIRECTION_START);
            final String windDirectionEnd = forecast.getString(K_WIND_DIRECTION_END);
            final int windSpeedMax = forecast.getInt(K_WIND_SPEED_MAX);
            final int windSpeedAvg = forecast.getInt(K_WIND_SPEED_AVG);
            final int temperatureMax = forecast.getInt(K_TEMPERATURE_MAX);
            final int temperatureMin = forecast.getInt(K_TEMPERATURE_MIN);
            ContentValues weatherValues = new ContentValues();
            weatherValues.put(WeatherEntry.COLUMN_CITY_FK, cityCode);
            weatherValues.put(WeatherEntry.COLUMN_FORECAST_DATE, forecastDateMillis);
            weatherValues.put(WeatherEntry.COLUMN_DAY_PERIOD, dayPeriod);
            weatherValues.put(WeatherEntry.COLUMN_ICON, icon);
            weatherValues.put(WeatherEntry.COLUMN_RAIN, rain);
            weatherValues.put(WeatherEntry.COLUMN_DESCRIPTION, description);
            weatherValues.put(WeatherEntry.COLUMN_LAST_UPDATE, lastUpdate);
            weatherValues.put(WeatherEntry.COLUMN_RELATIVE_HUMIDITY, humidity);
            weatherValues.put(WeatherEntry.COLUMN_TEMPERATURE_MAX, temperatureMax);
            weatherValues.put(WeatherEntry.COLUMN_TEMPERATURE_MIN, temperatureMin);
            weatherValues.put(WeatherEntry.COLUMN_WIND_DIRECTION_START, windDirectionStart);
            weatherValues.put(WeatherEntry.COLUMN_WIND_DIRECTION_END, windDirectionEnd);
            weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED_MAX, windSpeedMax);
            weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED_AVG, windSpeedAvg);
            cvsList.add(weatherValues);
        }
        return cvsList.toArray(new ContentValues[cvsList.size()]);
    }
/**
     * Parse weather data from JSON string
     *
     * @param jsonStr JSON string containing the weather forecast data for a given day and city
     * @param cityCode city ID
     * @return parsed values
     * @throws JSONException on error parsing JSON array of objects
     */
Parse weather data from JSON string

@param jsonStr JSON string containing the weather forecast data for a given day and city
@param cityCode city ID
@return parsed values
@throws JSONException on error parsing JSON array of objects
Parse weather data from JSON string @param jsonStr JSON string containing the weather forecast data for a given day and city @param cityCode city ID @return parsed values @throws JSONException on error parsing JSON array of objects
private ContentValues[] parseWeatherDataFromJson(String jsonStr, Integer cityCode) throws JSONException {
        if (jsonStr == null) {
            return null;
        }
        Log.i(TAG, jsonStr);
        final String K_ICON = "iconPrev";
        final String K_DAY_PERIOD = "periodoDia";
        final String K_RAIN = "mmChuva";
        final String K_DESCRIPTION = "nmCondicao";
        final String K_FORECAST_DATE = "dtPrev";
        final String K_LAST_UPDATE = "dtAtualizacao";
        final String K_RELATIVE_HUMIDITY = "umidadeRel";
        final String K_WIND_DIRECTION_START = "dirVentoIni";
        final String K_WIND_DIRECTION_END = "dirVentoFin";
        final String K_WIND_SPEED_MAX = "velVentoMax";
        final String K_WIND_SPEED_AVG = "velVentoMed";
        final String K_TEMPERATURE_MAX = "tempMax";
        final String K_TEMPERATURE_MIN = "tempMin";
        JSONArray weatherArray = new JSONArray(jsonStr);
        ArrayList<ContentValues> cvsList = new ArrayList<>(weatherArray.length());
        for (int i = 0; i < weatherArray.length(); ++i) {
            JSONObject forecast = weatherArray.getJSONObject(i);
            final int icon = forecast.getInt(K_ICON);
            if (icon == 0) continue; 
            final int dayPeriod = forecast.getInt(K_DAY_PERIOD);
            final int rain = forecast.getInt(K_RAIN);
            final String description = forecast.getString(K_DESCRIPTION);
            final long forecastDateMillis = forecast.getLong(K_FORECAST_DATE);
            final long lastUpdate = forecast.getLong(K_LAST_UPDATE);
            final int humidity = forecast.getInt(K_RELATIVE_HUMIDITY);
            final String windDirectionStart = forecast.getString(K_WIND_DIRECTION_START);
            final String windDirectionEnd = forecast.getString(K_WIND_DIRECTION_END);
            final int windSpeedMax = forecast.getInt(K_WIND_SPEED_MAX);
            final int windSpeedAvg = forecast.getInt(K_WIND_SPEED_AVG);
            final int temperatureMax = forecast.getInt(K_TEMPERATURE_MAX);
            final int temperatureMin = forecast.getInt(K_TEMPERATURE_MIN);
            ContentValues weatherValues = new ContentValues();
            weatherValues.put(WeatherEntry.COLUMN_CITY_FK, cityCode);
            weatherValues.put(WeatherEntry.COLUMN_FORECAST_DATE, forecastDateMillis);
            weatherValues.put(WeatherEntry.COLUMN_DAY_PERIOD, dayPeriod);
            weatherValues.put(WeatherEntry.COLUMN_ICON, icon);
            weatherValues.put(WeatherEntry.COLUMN_RAIN, rain);
            weatherValues.put(WeatherEntry.COLUMN_DESCRIPTION, description);
            weatherValues.put(WeatherEntry.COLUMN_LAST_UPDATE, lastUpdate);
            weatherValues.put(WeatherEntry.COLUMN_RELATIVE_HUMIDITY, humidity);
            weatherValues.put(WeatherEntry.COLUMN_TEMPERATURE_MAX, temperatureMax);
            weatherValues.put(WeatherEntry.COLUMN_TEMPERATURE_MIN, temperatureMin);
            weatherValues.put(WeatherEntry.COLUMN_WIND_DIRECTION_START, windDirectionStart);
            weatherValues.put(WeatherEntry.COLUMN_WIND_DIRECTION_END, windDirectionEnd);
            weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED_MAX, windSpeedMax);
            weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED_AVG, windSpeedAvg);
            cvsList.add(weatherValues);
        }
        return cvsList.toArray(new ContentValues[cvsList.size()]);
    }
private ContentValues [ ] parseWeatherDataFromJson ( String jsonStr , Integer cityCode ) throws JSONException { if ( jsonStr == null ) { return null ; } Log . i ( TAG , jsonStr ) ; final String K_ICON = "iconPrev" ; final String K_DAY_PERIOD = "periodoDia" ; final String K_RAIN = "mmChuva" ; final String K_DESCRIPTION = "nmCondicao" ; final String K_FORECAST_DATE = "dtPrev" ; final String K_LAST_UPDATE = "dtAtualizacao" ; final String K_RELATIVE_HUMIDITY = "umidadeRel" ; final String K_WIND_DIRECTION_START = "dirVentoIni" ; final String K_WIND_DIRECTION_END = "dirVentoFin" ; final String K_WIND_SPEED_MAX = "velVentoMax" ; final String K_WIND_SPEED_AVG = "velVentoMed" ; final String K_TEMPERATURE_MAX = "tempMax" ; final String K_TEMPERATURE_MIN = "tempMin" ; JSONArray weatherArray = new JSONArray ( jsonStr ) ; ArrayList < ContentValues > cvsList = new ArrayList < > ( weatherArray . length ( ) ) ; for ( int i = 0 ; i < weatherArray . length ( ) ; ++ i ) { JSONObject forecast = weatherArray . getJSONObject ( i ) ; final int icon = forecast . getInt ( K_ICON ) ; if ( icon == 0 ) continue ; final int dayPeriod = forecast . getInt ( K_DAY_PERIOD ) ; final int rain = forecast . getInt ( K_RAIN ) ; final String description = forecast . getString ( K_DESCRIPTION ) ; final long forecastDateMillis = forecast . getLong ( K_FORECAST_DATE ) ; final long lastUpdate = forecast . getLong ( K_LAST_UPDATE ) ; final int humidity = forecast . getInt ( K_RELATIVE_HUMIDITY ) ; final String windDirectionStart = forecast . getString ( K_WIND_DIRECTION_START ) ; final String windDirectionEnd = forecast . getString ( K_WIND_DIRECTION_END ) ; final int windSpeedMax = forecast . getInt ( K_WIND_SPEED_MAX ) ; final int windSpeedAvg = forecast . getInt ( K_WIND_SPEED_AVG ) ; final int temperatureMax = forecast . getInt ( K_TEMPERATURE_MAX ) ; final int temperatureMin = forecast . getInt ( K_TEMPERATURE_MIN ) ; ContentValues weatherValues = new ContentValues ( ) ; weatherValues . put ( WeatherEntry . COLUMN_CITY_FK , cityCode ) ; weatherValues . put ( WeatherEntry . COLUMN_FORECAST_DATE , forecastDateMillis ) ; weatherValues . put ( WeatherEntry . COLUMN_DAY_PERIOD , dayPeriod ) ; weatherValues . put ( WeatherEntry . COLUMN_ICON , icon ) ; weatherValues . put ( WeatherEntry . COLUMN_RAIN , rain ) ; weatherValues . put ( WeatherEntry . COLUMN_DESCRIPTION , description ) ; weatherValues . put ( WeatherEntry . COLUMN_LAST_UPDATE , lastUpdate ) ; weatherValues . put ( WeatherEntry . COLUMN_RELATIVE_HUMIDITY , humidity ) ; weatherValues . put ( WeatherEntry . COLUMN_TEMPERATURE_MAX , temperatureMax ) ; weatherValues . put ( WeatherEntry . COLUMN_TEMPERATURE_MIN , temperatureMin ) ; weatherValues . put ( WeatherEntry . COLUMN_WIND_DIRECTION_START , windDirectionStart ) ; weatherValues . put ( WeatherEntry . COLUMN_WIND_DIRECTION_END , windDirectionEnd ) ; weatherValues . put ( WeatherEntry . COLUMN_WIND_SPEED_MAX , windSpeedMax ) ; weatherValues . put ( WeatherEntry . COLUMN_WIND_SPEED_AVG , windSpeedAvg ) ; cvsList . add ( weatherValues ) ; } return cvsList . toArray ( new ContentValues [ cvsList . size ( ) ] ) ; }
Parse weather data from JSON string
Parse weather data from JSON string
// data unavailable, skip record
