parse 'service down' in JSON nearby stations

git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@836 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
andreas.schildbach@gmail.com 2011-10-21 20:17:18 +00:00
parent 3233daf902
commit a14c837b8b

View file

@ -1247,14 +1247,17 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
{ {
final CharSequence page = ParserUtils.scrape(uri, false, null, jsonEncoding, null); final CharSequence page = ParserUtils.scrape(uri, false, null, jsonEncoding, null);
final List<Location> stations = new ArrayList<Location>();
try try
{ {
final JSONObject head = new JSONObject(page.toString()); final JSONObject head = new JSONObject(page.toString());
final int error = head.getInt("error");
if (error == 0)
{
final JSONArray aStops = head.getJSONArray("stops"); final JSONArray aStops = head.getJSONArray("stops");
final int nStops = aStops.length();
final List<Location> stations = new ArrayList<Location>(nStops);
for (int i = 0; i < aStops.length(); i++) for (int i = 0; i < nStops; i++)
{ {
final JSONObject stop = aStops.optJSONObject(i); final JSONObject stop = aStops.optJSONObject(i);
final int id = stop.getInt("extId"); final int id = stop.getInt("extId");
@ -1269,14 +1272,23 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
stations.add(new Location(LocationType.STATION, id, lat, lon, placeAndName[0], placeAndName[1])); stations.add(new Location(LocationType.STATION, id, lat, lon, placeAndName[0], placeAndName[1]));
} }
} }
return new NearbyStationsResult(null, stations);
}
else if (error == 2)
{
return new NearbyStationsResult(null, NearbyStationsResult.Status.SERVICE_DOWN);
}
else
{
throw new RuntimeException("unknown error: " + error);
}
} }
catch (final JSONException x) catch (final JSONException x)
{ {
x.printStackTrace(); x.printStackTrace();
throw new RuntimeException("cannot parse: '" + page + "' on " + uri, x); throw new RuntimeException("cannot parse: '" + page + "' on " + uri, x);
} }
return new NearbyStationsResult(null, stations);
} }
private final static Pattern P_NEARBY_COARSE = Pattern.compile("<tr class=\"(zebra[^\"]*)\">(.*?)</tr>", Pattern.DOTALL); private final static Pattern P_NEARBY_COARSE = Pattern.compile("<tr class=\"(zebra[^\"]*)\">(.*?)</tr>", Pattern.DOTALL);