Back to project page android-nextbus.
The source code is released under:
MIT License
If you think the Android project android-nextbus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.andrei.nextbus.library.commands; /*from www. j a v a 2s .c om*/ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import com.andrei.nextbus.library.objects.Agency; import com.andrei.nextbus.library.objects.AgencyStopPair; import com.andrei.nextbus.library.objects.Direction; import com.andrei.nextbus.library.objects.MultiStopDirection; import com.andrei.nextbus.library.objects.MultiStopPredictions; import com.andrei.nextbus.library.objects.Path; import com.andrei.nextbus.library.objects.Prediction; import com.andrei.nextbus.library.objects.Route; import com.andrei.nextbus.library.objects.Stop; import com.andrei.nextbus.library.objects.Vehicle; import com.andrei.nextbus.library.parsers.Parser; import com.andrei.nextbus.library.parsers.XmlTagFilter; public class OnlineCommands extends Commands { private static URL createURL (String s){ try { return new URL (s); } catch (MalformedURLException e){ e.printStackTrace(); return null; } } public static List <Agency> getAgencies(){ URL url = createURL ("http://webservices.nextbus.com/service/publicXMLFeed?command=agencyList"); return getAgencies (Parser.getXmlAsString(url)); } public static List <Direction> getDirections(String agencyTag, String routeTag){ URL url = createURL ("http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a="+agencyTag+"&r="+routeTag); return getDirections (Parser.getXmlAsString(url)); } public static List <Path> getPathsForRoute (String agencyTag, String routeTag){ URL url = createURL("http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a="+agencyTag+"&r="+routeTag); return getPaths (Parser.getXmlAsString(url)); } public static List <Route> getRoutes(String agencyTag){ URL url = createURL("http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a="+agencyTag); return getRoutes (Parser.getXmlAsString(url)); } public static List <Stop> getAllStops(String agencyTag, String routeTag){ URL url = createURL("http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a="+agencyTag+"&r="+routeTag); return getAllStops (Parser.getXmlAsString(url)); } public static List <Stop> getAllStopsForDirection (String agencyTag, String routeTag, String directionTag){ URL url = createURL("http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a="+agencyTag+"&r="+routeTag); return getStopsForDirection(Parser.getXmlAsString(url), directionTag); } public static List <Prediction> getPredictionsForStop (String agencyTag, String routeTag, String stopTag){ URL url = createURL("http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a="+agencyTag+"&r="+routeTag+"&s="+stopTag); return getPredictionsForStop (Parser.getXmlAsString(url)); } public static List <Vehicle> getVehicles (String agencyTag, String routeTag, long timeFilter){ if (timeFilter < 0){ timeFilter = 0L; } URL url = createURL ("http://webservices.nextbus.com/service/publicXMLFeed?command=vehicleLocations&a="+agencyTag+"&r="+routeTag+"&t="+timeFilter); return getVehicles (Parser.getXmlAsString(url)); } public static List <MultiStopPredictions> getPredictionsForMultiStops (String agencyTag, AgencyStopPair ... stops){ StringBuilder s = new StringBuilder ("http://webservices.nextbus.com/service/publicXMLFeed?command=predictionsForMultiStops&a=") .append(agencyTag); for (int x = 0; x < stops.length;x++){ s.append("&stops=") .append (stops[x].getConcatedTuple()); } List <MultiStopPredictions> predictions = new ArrayList <MultiStopPredictions> (); URL url = createURL (s.toString()); String content = Parser.getXmlAsString(url); XmlTagFilter prediction = new XmlTagFilter (2, "predictions"); List <MultiStopPredictions> mspreds = Parser.parse(MultiStopPredictions.class,prediction, content); for (int x = 0; x < mspreds.size();x++){ MultiStopPredictions m = mspreds.get(x); prediction.setAttrributeSpec("stopTag", m.attribs.get("stopTag")); XmlTagFilter pred = new XmlTagFilter (4,"prediction"); List <Prediction> preds = Parser.parse(Prediction.class, pred, content, prediction); MultiStopDirection d = new MultiStopDirection (); d.predictions = preds; List <MultiStopDirection> l = new ArrayList <MultiStopDirection> (); l.add(d); m.directions = l; } return predictions; } }