Android Open Source - Blue-Sky-Weather Station Parser Task






From Project

Back to project page Blue-Sky-Weather.

License

The source code is released under:

GNU General Public License

If you think the Android project Blue-Sky-Weather 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

/**
 * Copyright (C) 2011 David Schonert//from   w ww  . j ava  2  s  . c om
 *
 * This file is part of BlueSky.
 *
 * BlueSky is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * BlueSky is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BlueSky.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.SyntheticCode.BlueSkyWeather;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import com.SyntheticCode.BlueSkyWeather.parsers.StationPullParser;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

/**
 * @author David
 * Starts an AsyncTask for parsing the list of stations.
 * Displays a ProgressDialog while parsing. Parse can be canceled with back button.
 */
public class StationParserTask extends AsyncTask<String, Void, StationList> {
  BlueSkyActivity parent = null;
  Context appContext = null;

  private StationPullParser parser;

  public StationParserTask(BlueSkyActivity activity) {
    // Get the application context so that if the activity is destroyed
    // while thread is running the context will not be null.
    appContext = activity.getApplicationContext();
    
    attach(activity);
  }
  
  public void attach(BlueSkyActivity activity) {
    this.parent = activity;
  }
  
  public void detach() {
    this.parent = null;
  }
  
  
  protected void onPreExecute() {
    parent.showDialog(BlueSkyActivity.DIALOG_STATION_LOADING);
  }
  
  protected StationList doInBackground(String... params) {
    StationList list = null;
    
    // Try creating the parser and then parsing
    try {
      parser = new StationPullParser(this.appContext, params[0]);
      list = parser.parse();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    
    return list;
  }
  
  protected void onPostExecute(StationList result) {
    if(result != null) {
      // Update stations with result
      parent.stationList = result;
      ArrayList<String> stations = parent.stationList.getStationNamesList();

      // Update the station spinner with list of PWS stations
      parent.updateStationList(stations);
    }
    else {
      // Warn user
      Toast.makeText(parent, "Network Error", Toast.LENGTH_LONG).show();
    }
    
    // Close the dialog
    parent.dismissDialog(BlueSkyActivity.DIALOG_STATION_LOADING);
  }
  
  public boolean stopParsing() {
    if(parser != null) {
      parser.stopParse();
    }
    // Cancel the AsyncTask (isCancled() needs to be checked during doInBackground())
    return cancel(true);
  }
}




Java Source Code List

com.SyntheticCode.BlueSkyWeather.AboutActivity.java
com.SyntheticCode.BlueSkyWeather.BlueSkyActivity.java
com.SyntheticCode.BlueSkyWeather.CityData.java
com.SyntheticCode.BlueSkyWeather.ForecastData.java
com.SyntheticCode.BlueSkyWeather.ForecastParserTask.java
com.SyntheticCode.BlueSkyWeather.ForecastShortView.java
com.SyntheticCode.BlueSkyWeather.SearchResultActivity.java
com.SyntheticCode.BlueSkyWeather.SettingsActivity.java
com.SyntheticCode.BlueSkyWeather.StationList.java
com.SyntheticCode.BlueSkyWeather.StationParserTask.java
com.SyntheticCode.BlueSkyWeather.UiObjects.java
com.SyntheticCode.BlueSkyWeather.WeatherData.java
com.SyntheticCode.BlueSkyWeather.WeatherParserTask.java
com.SyntheticCode.BlueSkyWeather.WeatherStation.java
com.SyntheticCode.BlueSkyWeather.parsers.BaseFeedParser.java
com.SyntheticCode.BlueSkyWeather.parsers.ForecastParser.java
com.SyntheticCode.BlueSkyWeather.parsers.GeoLookupParser.java
com.SyntheticCode.BlueSkyWeather.parsers.StationPullParser.java