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

166 lines
4.8 KiB
Java
Raw Normal View History

2011-05-23 18:59:34 +00:00
package com.dougkeen.bart;
import java.io.IOException;
2011-06-17 21:35:20 +00:00
import java.io.StringWriter;
2011-05-23 18:59:34 +00:00
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.List;
2011-06-17 21:35:20 +00:00
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
2011-05-23 18:59:34 +00:00
import org.xml.sax.SAXException;
import android.os.AsyncTask;
2011-06-17 21:35:20 +00:00
import android.util.Log;
2011-05-23 18:59:34 +00:00
import android.util.Xml;
2011-06-17 21:35:20 +00:00
import com.dougkeen.bart.data.RealTimeDepartures;
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";
2011-06-20 22:34:09 +00:00
private final static int MAX_ATTEMPTS = 5;
2011-05-23 18:59:34 +00:00
2011-06-17 21:35:20 +00:00
private Exception mException;
2011-05-23 18:59:34 +00:00
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-06-17 21:35:20 +00:00
String xml = null;
2011-05-23 18:59:34 +00:00
try {
2011-06-17 21:35:20 +00:00
HttpUriRequest request = new HttpGet(String.format(API_URL,
2011-06-01 03:42:32 +00:00
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-17 21:35:20 +00:00
HttpResponse response = executeWithRecovery(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new IOException("Server returned "
+ response.getStatusLine().toString());
}
StringWriter writer = new StringWriter();
IOUtils.copy(response.getEntity().getContent(), writer, "UTF-8");
xml = writer.toString();
if (xml.length() == 0) {
throw new IOException("Server returned blank xml document");
}
Xml.parse(xml, 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 {
2011-06-17 21:35:20 +00:00
Log.w(Constants.TAG,
2011-06-20 22:34:09 +00:00
"Attempt to contact server failed... retrying in 3s",
2011-06-17 21:35:20 +00:00
e);
2011-06-20 22:34:09 +00:00
Thread.sleep(3000);
2011-05-23 18:59:34 +00:00
} catch (InterruptedException interrupt) {
// Ignore... just go on to next attempt
}
return getDeparturesFromNetwork(params, attemptNumber + 1);
2011-05-23 18:59:34 +00:00
} else {
2011-06-17 21:35:20 +00:00
mException = new Exception("Could not contact BART system", e);
2011-05-23 18:59:34 +00:00
return null;
}
} catch (SAXException e) {
2011-06-17 21:35:20 +00:00
mException = new Exception(
"Could not understand response from BART system: " + xml, e);
return null;
2011-05-23 18:59:34 +00:00
}
}
2011-06-17 21:35:20 +00:00
private static HttpResponse executeWithRecovery(final HttpUriRequest request)
throws IOException, ClientProtocolException {
try {
return getHttpClient().execute(request);
} catch (IllegalStateException e) {
// try again... this is a rare error
return getHttpClient().execute(request);
}
}
private static HttpClient getHttpClient() {
HttpClient client = new DefaultHttpClient();
final HttpParams params = client.getParams();
HttpConnectionParams.setConnectionTimeout(params,
CONNECTION_TIMEOUT_MILLIS);
HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT_MILLIS);
ConnManagerParams.setTimeout(params, CONNECTION_TIMEOUT_MILLIS);
return client;
}
2011-05-23 18:59:34 +00:00
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 {
2011-06-17 21:35:20 +00:00
onError(mException);
2011-05-23 18:59:34 +00:00
}
}
public abstract void onResult(RealTimeDepartures result);
2011-05-23 18:59:34 +00:00
2011-06-17 21:35:20 +00:00
public abstract void onError(Exception exception);
2011-05-23 18:59:34 +00:00
}