From a14c837b8bee40f8991dd9b43071a2b02abe53ee Mon Sep 17 00:00:00 2001 From: "andreas.schildbach@gmail.com" Date: Fri, 21 Oct 2011 20:17:18 +0000 Subject: [PATCH] parse 'service down' in JSON nearby stations git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@836 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- .../schildbach/pte/AbstractHafasProvider.java | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/de/schildbach/pte/AbstractHafasProvider.java b/src/de/schildbach/pte/AbstractHafasProvider.java index b5db1cf5..1837f3a9 100644 --- a/src/de/schildbach/pte/AbstractHafasProvider.java +++ b/src/de/schildbach/pte/AbstractHafasProvider.java @@ -1247,27 +1247,41 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider { final CharSequence page = ParserUtils.scrape(uri, false, null, jsonEncoding, null); - final List stations = new ArrayList(); - try { final JSONObject head = new JSONObject(page.toString()); - final JSONArray aStops = head.getJSONArray("stops"); - - for (int i = 0; i < aStops.length(); i++) + final int error = head.getInt("error"); + if (error == 0) { - final JSONObject stop = aStops.optJSONObject(i); - final int id = stop.getInt("extId"); - final String name = ParserUtils.resolveEntities(stop.getString("name")); - final int lat = stop.getInt("y"); - final int lon = stop.getInt("x"); - final int stopWeight = stop.optInt("stopweight", -1); + final JSONArray aStops = head.getJSONArray("stops"); + final int nStops = aStops.length(); + final List stations = new ArrayList(nStops); - if (stopWeight != 0) + for (int i = 0; i < nStops; i++) { - final String[] placeAndName = splitPlaceAndName(name); - stations.add(new Location(LocationType.STATION, id, lat, lon, placeAndName[0], placeAndName[1])); + final JSONObject stop = aStops.optJSONObject(i); + final int id = stop.getInt("extId"); + final String name = ParserUtils.resolveEntities(stop.getString("name")); + final int lat = stop.getInt("y"); + final int lon = stop.getInt("x"); + final int stopWeight = stop.optInt("stopweight", -1); + + if (stopWeight != 0) + { + final String[] placeAndName = splitPlaceAndName(name); + 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) @@ -1275,8 +1289,6 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider x.printStackTrace(); throw new RuntimeException("cannot parse: '" + page + "' on " + uri, x); } - - return new NearbyStationsResult(null, stations); } private final static Pattern P_NEARBY_COARSE = Pattern.compile("(.*?)", Pattern.DOTALL);