Android Open Source - UK-Weather-repo Worker Fragment To Retrieve Json String






From Project

Back to project page UK-Weather-repo.

License

The source code is released under:

Apache License

If you think the Android project UK-Weather-repo 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.haringeymobile.ukweather;
/*  w  w  w . j  a  va2  s.  c om*/
import java.io.IOException;
import java.net.URL;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.haringeymobile.ukweather.data.JsonFetcher;
import com.haringeymobile.ukweather.database.CityTable;
import com.haringeymobile.ukweather.database.SqlOperation;
import com.haringeymobile.ukweather.utils.AsyncTaskWithProgressBar;
import com.haringeymobile.ukweather.utils.MiscMethods;
import com.haringeymobile.ukweather.utils.SharedPrefsHelper;

/**
 * A fragment to asynchronously obtain the specified JSON weather data. This
 * fragment has no UI, and simply acts as an executor of the
 * {@link RetrieveWeatherInformationJsonStringTask}.
 */
public class WorkerFragmentToRetrieveJsonString extends Fragment {

  /** A listener for the requested JSON weather data retrieval. */
  public interface OnJsonStringRetrievedListener {

    /**
     * Reacts to the JSON weather information retrieval.
     * 
     * @param jsonString
     *            JSON weather data
     * @param weatherInfoType
     *            a kind of weather information
     * @param shouldSaveLocally
     *            whether the retrieved data should be saved in the database
     */
    public abstract void onJsonStringRetrieved(String jsonString,
        WeatherInfoType weatherInfoType, boolean shouldSaveLocally);

  }

  private Activity parentActivity;
  private OnJsonStringRetrievedListener callbackToParentActivity;
  /** An Open Weather Map city ID. */
  private int cityId;
  private WeatherInfoType weatherInfoType;
  private RetrieveWeatherInformationJsonStringTask retrieveWeatherInformationJsonStringTask;

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    parentActivity = activity;
    try {
      callbackToParentActivity = (OnJsonStringRetrievedListener) activity;
    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString()
          + " must implement Listener");
    }
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
  }

  /**
   * Repeats the last weather data request, i.e. retrieves the weather
   * information using parameters (city and information type) used for last
   * attempt to obtain weather information.
   */
  void retrieveLastRequestedWeatherInfo() {
    int lastCityId = SharedPrefsHelper
        .getCityIdFromSharedPrefs(parentActivity);
    if (lastCityId != CityTable.CITY_ID_DOES_NOT_EXIST) {
      WeatherInfoType lastWeatherInfoType = SharedPrefsHelper
          .getLastWeatherInfoTypeFromSharedPrefs(parentActivity);
      retrieveWeatherInfoJsonString(lastCityId, lastWeatherInfoType);
    }
  }

  /**
   * Starts an {@link AsyncTask} to obtain the requested JSON weather data.
   * 
   * @param cityId
   *            an Open Weather Map city ID
   * @param weatherInfoType
   *            a type of the requested weather data
   */
  void retrieveWeatherInfoJsonString(int cityId,
      WeatherInfoType weatherInfoType) {
    if (MiscMethods.isUserOnline(parentActivity)) {
      this.cityId = cityId;
      this.weatherInfoType = weatherInfoType;

      URL openWeatherMapUrl = weatherInfoType
          .getOpenWeatherMapUrl(cityId);
      retrieveWeatherInformationJsonStringTask = new RetrieveWeatherInformationJsonStringTask();
      retrieveWeatherInformationJsonStringTask.setContext(parentActivity);
      retrieveWeatherInformationJsonStringTask.execute(openWeatherMapUrl);
    } else {
      Toast.makeText(parentActivity, R.string.error_message,
          Toast.LENGTH_SHORT).show();
    }
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    if (retrieveWeatherInformationJsonStringTask != null) {
      retrieveWeatherInformationJsonStringTask.cancel(true);
    }
  }

  @Override
  public void onDetach() {
    super.onDetach();
    parentActivity = null;
    callbackToParentActivity = null;
  }

  /**
   * A task to obtain JSON weather data from the provided Open Weather Map
   * URL.
   * <p>
   * <p>
   * Since weather data doesn't change that often, retrieved JSON strings are
   * saved locally, and reused for a short period of time (chosen by user). So
   * the task first checks if there already exists a recently requested and
   * saved data in the local SQLite Database, and connects to internet only if
   * such data is too old or does not exist.
   */
  private class RetrieveWeatherInformationJsonStringTask extends
      AsyncTaskWithProgressBar<URL, Void, String> {

    /**
     * If the data is obtained from the web service, it should be saved and
     * reused for a short period of time for efficiency reasons.
     */
    private boolean saveDataLocally = false;

    @Override
    protected String doInBackground(URL... params) {
      SqlOperation sqlOperation = new SqlOperation(parentActivity,
          weatherInfoType);
      String jsonString = sqlOperation
          .getJsonStringForWeatherInfo(cityId);
      if (jsonString == null && !isCancelled()) {
        jsonString = getJSONStringFromWebService(params[0]);
        saveDataLocally = jsonString != null;
      }
      return jsonString;
    }

    /**
     * Attempts to obtain JSON weather data from the provided Open Weather
     * Map URL
     * 
     * @param url
     *            Open Weather Map URL
     * @return JSON data for the requested city and weather information
     *         type, or {@code null} in case of network problems
     */
    private String getJSONStringFromWebService(URL url) {
      try {
        return new JsonFetcher().getJsonString(url);
      } catch (IOException e) {
        MiscMethods.log("IOException in getJSONStringFromWebService()");
        e.printStackTrace();
        return null;
      }
    }

    @Override
    protected void onPostExecute(String result) {
      super.onPostExecute(result);
      if (result == null) {
        if (parentActivity != null) {
          Toast.makeText(parentActivity, R.string.error_message,
              Toast.LENGTH_SHORT).show();
        }
      } else if (callbackToParentActivity != null) {
        callbackToParentActivity.onJsonStringRetrieved(result,
            weatherInfoType, saveDataLocally);
      }
    }

  }

}




Java Source Code List

com.astuetz.PagerSlidingTabStrip.java
com.haringeymobile.ukweather.AboutActivity.java
com.haringeymobile.ukweather.BaseCityCursorAdapter.java
com.haringeymobile.ukweather.BaseCityListFragmentWithButtons.java
com.haringeymobile.ukweather.CityListFragmentWithUtilityButtons.java
com.haringeymobile.ukweather.CityListFragmentWithWeatherButtons.java
com.haringeymobile.ukweather.CityManagementActivity.java
com.haringeymobile.ukweather.CitySearchResultsDialog.java
com.haringeymobile.ukweather.CityUtilitiesCursorAdapter.java
com.haringeymobile.ukweather.CityWeatherCursorAdapter.java
com.haringeymobile.ukweather.DeleteCityDialog.java
com.haringeymobile.ukweather.GetAvailableCitiesTask.java
com.haringeymobile.ukweather.MainActivity.java
com.haringeymobile.ukweather.SettingsActivityPreHoneycomb.java
com.haringeymobile.ukweather.SettingsActivity.java
com.haringeymobile.ukweather.SettingsFragment.java
com.haringeymobile.ukweather.WeatherCurrentInfoFragment.java
com.haringeymobile.ukweather.WeatherDailyWeatherForecastChildFragment.java
com.haringeymobile.ukweather.WeatherForecastParentFragment.java
com.haringeymobile.ukweather.WeatherInfoActivity.java
com.haringeymobile.ukweather.WeatherInfoFragment.java
com.haringeymobile.ukweather.WeatherInfoType.java
com.haringeymobile.ukweather.WeatherThreeHourlyForecastChildFragment.java
com.haringeymobile.ukweather.WorkerFragmentToRetrieveJsonString.java
com.haringeymobile.ukweather.data.InitialCity.java
com.haringeymobile.ukweather.data.JsonFetcher.java
com.haringeymobile.ukweather.data.OpenWeatherMapUrl.java
com.haringeymobile.ukweather.data.objects.CityCurrentWeather.java
com.haringeymobile.ukweather.data.objects.CityDailyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.CityInfo.java
com.haringeymobile.ukweather.data.objects.CityThreeHourlyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.Clouds.java
com.haringeymobile.ukweather.data.objects.Coordinates.java
com.haringeymobile.ukweather.data.objects.NumericParameters.java
com.haringeymobile.ukweather.data.objects.Rain.java
com.haringeymobile.ukweather.data.objects.SearchResponseForDailyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForFindQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForThreeHourlyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SystemParameters.java
com.haringeymobile.ukweather.data.objects.TemperatureScale.java
com.haringeymobile.ukweather.data.objects.Temperature.java
com.haringeymobile.ukweather.data.objects.WeatherInformation.java
com.haringeymobile.ukweather.data.objects.Weather.java
com.haringeymobile.ukweather.data.objects.WindSpeedMeasurementUnit.java
com.haringeymobile.ukweather.data.objects.Wind.java
com.haringeymobile.ukweather.database.CityTable.java
com.haringeymobile.ukweather.database.DatabaseHelper.java
com.haringeymobile.ukweather.database.GeneralDatabaseService.java
com.haringeymobile.ukweather.database.SqlOperation.java
com.haringeymobile.ukweather.database.WeatherContentProvider.java
com.haringeymobile.ukweather.utils.AsyncTaskWithProgressBar.java
com.haringeymobile.ukweather.utils.GlobalConstants.java
com.haringeymobile.ukweather.utils.MiscMethods.java
com.haringeymobile.ukweather.utils.SharedPrefsHelper.java