Better handling of BART API errors
This commit is contained in:
parent
226104ea10
commit
44b1a1983a
@ -0,0 +1,7 @@
|
||||
package com.dougkeen.bart.networktasks;
|
||||
|
||||
public class BartApiException extends Exception {
|
||||
public BartApiException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ public class EtdContentHandler extends DefaultHandler {
|
||||
|
||||
private final static List<String> TAGS = Arrays.asList("date", "time",
|
||||
"abbreviation", "minutes", "platform", "direction", "length",
|
||||
"color", "hexcolor", "bikeflag", "destination");
|
||||
"color", "hexcolor", "bikeflag", "destination", "error");
|
||||
|
||||
private RealTimeDepartures realTimeDepartures;
|
||||
|
||||
@ -41,6 +41,7 @@ public class EtdContentHandler extends DefaultHandler {
|
||||
private String currentValue;
|
||||
private Departure currentDeparture;
|
||||
private boolean isParsingTag;
|
||||
private String error;
|
||||
|
||||
@Override
|
||||
public void characters(char[] ch, int start, int length)
|
||||
@ -125,8 +126,14 @@ public class EtdContentHandler extends DefaultHandler {
|
||||
currentDestinationAbbreviation = null;
|
||||
} else if (localName.equals("station")) {
|
||||
realTimeDepartures.finalizeDeparturesList();
|
||||
} else if (localName.equals("error")) {
|
||||
error = currentValue;
|
||||
}
|
||||
isParsingTag = false;
|
||||
currentValue = null;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
@ -111,6 +111,12 @@ public abstract class GetRealTimeDeparturesTask extends
|
||||
}
|
||||
final RealTimeDepartures realTimeDepartures = handler
|
||||
.getRealTimeDepartures();
|
||||
|
||||
if (handler.getError() != null) {
|
||||
mException = new BartApiException("BART's systems are reporting a problem:\n\"" + handler.getError() + "\"");
|
||||
return null;
|
||||
}
|
||||
|
||||
return realTimeDepartures;
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
@ -25,6 +25,7 @@ import com.dougkeen.bart.model.ScheduleInformation;
|
||||
import com.dougkeen.bart.model.ScheduleItem;
|
||||
import com.dougkeen.bart.model.Station;
|
||||
import com.dougkeen.bart.model.StationPair;
|
||||
import com.dougkeen.bart.networktasks.BartApiException;
|
||||
import com.dougkeen.bart.networktasks.GetRealTimeDeparturesTask;
|
||||
import com.dougkeen.bart.networktasks.GetScheduleInformationTask;
|
||||
import com.googlecode.androidannotations.annotations.EService;
|
||||
@ -115,6 +116,8 @@ public class EtdService extends Service {
|
||||
private List<Departure> mLatestDepartures;
|
||||
private ScheduleInformation mLatestScheduleInfo;
|
||||
|
||||
private String mLatestDepartureError;
|
||||
|
||||
private AsyncTask<StationPair, Integer, RealTimeDepartures> mGetDeparturesTask;
|
||||
private AsyncTask<StationPair, Integer, ScheduleInformation> mGetScheduleInformationTask;
|
||||
|
||||
@ -139,6 +142,12 @@ public class EtdService extends Service {
|
||||
mStarted = true;
|
||||
fetchLatestDepartures();
|
||||
}
|
||||
// Replay ETD or error event
|
||||
if (!mLatestDepartures.isEmpty()) {
|
||||
listener.onETDChanged(mLatestDepartures);
|
||||
} else if (mLatestDepartureError != null) {
|
||||
listener.onError(mLatestDepartureError);
|
||||
}
|
||||
}
|
||||
|
||||
protected void unregisterListener(EtdServiceListener listener) {
|
||||
@ -195,6 +204,7 @@ public class EtdService extends Service {
|
||||
mIgnoreDepartureDirection) {
|
||||
@Override
|
||||
public void onResult(RealTimeDepartures result) {
|
||||
mLatestDepartureError = null;
|
||||
Log.v(Constants.TAG, "Processing data from server");
|
||||
processLatestDepartures(result);
|
||||
Log.v(Constants.TAG, "Done processing data from server");
|
||||
@ -205,7 +215,14 @@ public class EtdService extends Service {
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
Log.w(Constants.TAG, e.getMessage(), e);
|
||||
notifyListenersOfError(getString(R.string.could_not_connect));
|
||||
|
||||
if (e instanceof BartApiException) {
|
||||
mLatestDepartureError = e.getMessage();
|
||||
} else {
|
||||
mLatestDepartureError = getString(R.string.could_not_connect);
|
||||
}
|
||||
notifyListenersOfError(mLatestDepartureError);
|
||||
|
||||
// Try again in 60s
|
||||
scheduleDepartureFetch(60000);
|
||||
notifyListenersOfRequestEnd();
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:orientation="vertical">
|
||||
@ -20,14 +19,14 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:padding="10dp"
|
||||
android:visibility="gone"
|
||||
/>
|
||||
android:visibility="gone" />
|
||||
|
||||
<TextView
|
||||
android:id="@android:id/empty"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1" />
|
||||
android:layout_weight="1"
|
||||
android:padding="10dp" />
|
||||
|
||||
<ListView
|
||||
android:id="@android:id/list"
|
||||
@ -42,5 +41,5 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:indeterminate="true"
|
||||
android:visibility="gone" />
|
||||
F
|
||||
F
|
||||
</LinearLayout>
|
||||
|
Loading…
Reference in New Issue
Block a user