BartRunnerAndroid/src/com/dougkeen/bart/GetRealTimeDeparturesTask.java

116 lines
3.1 KiB
Java
Raw Normal View History

2011-05-23 18:59:34 +00:00
package com.dougkeen.bart;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
2011-06-01 03:42:32 +00:00
import java.net.URLConnection;
2011-05-23 18:59:34 +00:00
import java.util.List;
import org.xml.sax.SAXException;
import com.dougkeen.bart.data.RealTimeDepartures;
2011-05-23 18:59:34 +00:00
import android.os.AsyncTask;
import android.util.Xml;
public abstract class GetRealTimeDeparturesTask extends
AsyncTask<GetRealTimeDeparturesTask.Params, Integer, RealTimeDepartures> {
2011-05-23 18:59:34 +00:00
2011-06-01 03:42:32 +00:00
private static final int CONNECTION_TIMEOUT_MILLIS = 10000;
2011-05-23 18:59:34 +00:00
private final static String API_KEY = "5LD9-IAYI-TRAT-MHHW";
private final static String API_URL = "http://api.bart.gov/api/etd.aspx?cmd=etd&key="
+ API_KEY + "&orig=%1$s&dir=%2$s";
private final static int MAX_ATTEMPTS = 3;
private IOException mIOException;
private List<Route> mRoutes;
@Override
protected RealTimeDepartures doInBackground(Params... paramsArray) {
2011-05-23 18:59:34 +00:00
// Always expect one param
Params params = paramsArray[0];
mRoutes = params.origin.getRoutesForDestination(params.destination);
if (!isCancelled()) {
return getDeparturesFromNetwork(params, 0);
} else {
return null;
}
2011-05-23 18:59:34 +00:00
}
private RealTimeDepartures getDeparturesFromNetwork(Params params,
2011-06-01 03:42:32 +00:00
int attemptNumber) {
2011-05-23 18:59:34 +00:00
try {
2011-06-01 03:42:32 +00:00
URL sourceUrl = new URL(String.format(API_URL,
params.origin.abbreviation, mRoutes.get(0).getDirection()));
2011-05-23 18:59:34 +00:00
EtdContentHandler handler = new EtdContentHandler(params.origin,
params.destination, mRoutes);
if (isCancelled()) {
return null;
}
2011-06-01 03:42:32 +00:00
URLConnection connection = sourceUrl.openConnection();
connection.setConnectTimeout(CONNECTION_TIMEOUT_MILLIS);
Xml.parse(connection.getInputStream(),
Xml.findEncodingByName("UTF-8"),
2011-05-23 18:59:34 +00:00
handler);
final RealTimeDepartures realTimeDepartures = handler
.getRealTimeDepartures();
return realTimeDepartures;
2011-05-23 18:59:34 +00:00
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
if (attemptNumber < MAX_ATTEMPTS - 1) {
try {
Thread.sleep(5000);
} catch (InterruptedException interrupt) {
// Ignore... just go on to next attempt
}
return getDeparturesFromNetwork(params, attemptNumber + 1);
2011-05-23 18:59:34 +00:00
} else {
mIOException = e;
return null;
}
} catch (SAXException e) {
throw new RuntimeException(e);
}
}
public static class Params {
public Params(Station origin, Station destination) {
super();
this.origin = origin;
this.destination = destination;
}
private Station origin;
private Station destination;
public Station getOrigin() {
return origin;
}
public Station getDestination() {
return destination;
}
}
@Override
protected void onPostExecute(RealTimeDepartures result) {
2011-05-23 18:59:34 +00:00
if (result != null) {
onResult(result);
} else {
onNetworkError(mIOException);
}
}
public abstract void onResult(RealTimeDepartures result);
2011-05-23 18:59:34 +00:00
public abstract void onNetworkError(IOException e);
}