Android Open Source - Realtime-Port-Authority Port Authority Realtime






From Project

Back to project page Realtime-Port-Authority.

License

The source code is released under:

Apache License

If you think the Android project Realtime-Port-Authority listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
//from w w  w  .j  ava  2s.  c  o  m
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

class Bus {

  String vid;
  String lat;
  String lon;
  String msg;
  String tmstmp;
  String hdg;
  String pid;
  String rt;
  String des;
  String pdist;
  String spd;
  String tablockid;
  String tatripid;

  public float getLat() {
    return Float.parseFloat(lat);
  }

  public float getLon() {
    return Float.parseFloat(lon);
  }

  public int getVid() {
    return Integer.parseInt(vid);
  }

  @Override
  public String toString() {
    return vid + "\t" + lat + "\t" + lon + "\t" + tmstmp + "\t" + hdg
        + "\t" + pid + "\t" + rt + "\t" + des + "\t" + pdist + "\t"
        + spd + "\t" + tablockid + "\t" + tatripid;
  }
}

public class PortAuthorityRealtime extends TimerTask {

  public static String[] argums;

  public static void main(String[] args) throws Exception {
    PortAuthorityRealtime.argums = args;
    Timer timer = new Timer();
    timer.schedule(new PortAuthorityRealtime(), 0, 10000);
  }

  @Override
  public void run() {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = null;
    try {
      sp = spf.newSAXParser();
    } catch (ParserConfigurationException | SAXException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    URL url = null;
    try {
      url = new URL(
          "http://realtime.portauthority.org/bustime/api/v2/getvehicles?key="
              + argums[0] + "&rt=" + argums[1]);
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SAXHandler handler = new SAXHandler();
    try {
      try {
        sp.parse(new InputSource(url.openStream()), handler);
      } catch (SAXException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } catch (NullPointerException sax) {
      System.out
      .println("Bus route is not tracked or all buses on route are in garage.");
      System.exit(0);
    }

    for (Bus bus : handler.busList) {
      System.out.println(bus.toString());
    }
    System.out.println();
  }

}

/**
 * The Handler for SAX Events.
 */
class SAXHandler extends DefaultHandler {

  List<Bus> busList = new ArrayList<>();
  Bus bus = null;
  String content = null;

  @Override
  public void characters(char[] ch, int start, int length)
      throws SAXException {
    content = String.copyValueOf(ch, start, length).trim();
  }

  @Override
  public void endElement(String uri, String localName, String qName)
      throws SAXException {
    switch (qName) {
    // Add the bus to list once end tag is found
    case "vid":
      busList.add(bus);
      bus.vid = content;
      break;
      // For all other end tags the bus has to be updated.
    case "lat":
      bus.lat = content;
      break;
    case "lon":
      bus.lon = content;
      break;
    case "msg":
      bus.msg = content;
      break;
    case "tmstmp":
      bus.tmstmp = content;
      break;
    case "hdg":
      bus.hdg = content;
      break;
    case "pid":
      bus.pid = content;
      break;
    case "rt":
      bus.rt = content;
      break;
    case "des":
      bus.des = content;
      break;
    case "pdist":
      bus.pdist = content;
      break;
    case "spd":
      bus.spd = content;
      break;
    case "tablockid":
      bus.tablockid = content;
      break;
    case "tatripid":
      bus.tatripid = content;
      break;
    }
  }

  @Override
  // Triggered when the start of tag is found.
  public void startElement(String uri, String localName, String qName,
      Attributes attributes) throws SAXException {

    switch (qName) {
    // Create a new Bus object when the start tag is found
    case "vid":
      bus = new Bus();
      bus.vid = attributes.getValue("id");
      break;
    }
  }

}




Java Source Code List

.Globals.java
.PortAuthorityRealtime.java
rectangledbmi.com.pittsburghrealtimetracker.AboutActivity.java
rectangledbmi.com.pittsburghrealtimetracker.ApplicationTest.java
rectangledbmi.com.pittsburghrealtimetracker.BusInformationDialog.java
rectangledbmi.com.pittsburghrealtimetracker.NavigationDrawerFragment.java
rectangledbmi.com.pittsburghrealtimetracker.SelectTransit.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.BusNotRunningException.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.BusSaxHandler.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.BusXMLPullParser.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.InputSave.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.PredictionsXMLPullParser.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RequestLine.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RequestPredictions.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RequestRoutes.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RequestTask.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RouteSaxHandler.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.TransitSAXHandler.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.containers.ETAContainer.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.containers.RequestLineContainer.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.extend.CheckableRelativeLayout.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.extend.ColoredArrayAdapter.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.extend.ETAWindowAdapter.java
rectangledbmi.com.pittsburghrealtimetracker.world.Bus.java
rectangledbmi.com.pittsburghrealtimetracker.world.LineInfo.java
rectangledbmi.com.pittsburghrealtimetracker.world.Prediction.java
rectangledbmi.com.pittsburghrealtimetracker.world.Route.java
rectangledbmi.com.pittsburghrealtimetracker.world.TransitStop.java