Android Open Source - Realtime-Port-Authority Predictions X M L Pull Parser






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

package rectangledbmi.com.pittsburghrealtimetracker.handlers;
//from ww w .j a v  a  2  s.c om
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import rectangledbmi.com.pittsburghrealtimetracker.world.Prediction;

/**
 * Created by Ritwik Gupta on 12/17/14.
 */
public class PredictionsXMLPullParser {

    private List<Prediction> predictionList;
    private URL url;
    XmlPullParser parser;
    Context context; //Application context passed in

    public PredictionsXMLPullParser(URL url, Context context) throws XmlPullParserException {

        this.url = url;
        XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
        parser = pullParserFactory.newPullParser();
        this.context = context;
        predictionList = null;
    }

    /**
     * Creates the bus list by starting the XMLPullParser
     *
     * @return the list of predictions
     * @throws java.io.IOException
     * @throws XmlPullParserException
     */
    public List<Prediction> createPredictionList() throws IOException, XmlPullParserException {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setConnectTimeout(5000);
        conn.setUseCaches(false);
        InputStream in = conn.getInputStream();
        if(in != null) {
            predictionList = new LinkedList<>();
            parser.setInput(conn.getInputStream(), null);
            parseXML();
        } else {
            Toast.makeText(context, "Connection Timeout, Internet problem", Toast.LENGTH_LONG).show();
        }
        return getPredictionList();
    }

    /**
     * @return the list of buses
     */
    public List<Prediction> getPredictionList() {
        return predictionList;
    }

    /**
     * XMLPullParser implementation of getting predictions from the Port Authority API
     *
     * @throws XmlPullParserException
     * @throws IOException
     */
    private void parseXML() throws XmlPullParserException, IOException {

        int eventType = parser.getEventType();
        Prediction prediction = new Prediction();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            String name = parser.getName();

            try {
                switch (eventType) {

                    case (XmlPullParser.START_TAG): {
//                        if ("stpid".equals(name)) { //new vehicle seen and make sure nothing else is there
//                            prediction = new Prediction();
//                            prediction.setStpid(parser.nextText());
//                        } else {
                            //below is to add a new vehicle
//                        Log.i("predictions_xml_start_tag", name);
                            switch (name) {
                                case "prd":
                                    prediction = new Prediction();
                                    break;
                                case "vid":
                                    prediction.setVid(parser.nextText());
                                    break;
                                case "tmstmp":
                                    prediction.setTmpstmp(parser.nextText());
                                    break;
                                case "rt":
                                    prediction.setRt(parser.nextText());
                                    break;
                                case "des":
                                    prediction.setDes(parser.nextText());
                                    break;
                                case "dly":
                                    prediction.setDly(parser.nextText());
                                    break;
                                case "tablockid":
                                    prediction.setTablockid(parser.nextText());
                                    break;
                                case "tatripid":
                                    prediction.setTatripid(parser.nextText());
                                    break;
                                case "typ":
                                    prediction.setTyp(parser.nextText());
                                    break;
                                case "stpnm":
                                    prediction.setStpnm(parser.nextText());
                                    break;
                                case "stpid":
                                    prediction.setStpid(parser.nextText());
                                    break;
                                case "dstp":
                                    prediction.setDstp(parser.nextText());
                                    break;
                                case "rtdir":
                                    prediction.setRtdir(parser.nextText());
                                    break;
                                case "prdtm":
                                    prediction.setPrdtm(parser.nextText());
                                    break;
                                case "prtctdn":
                                    prediction.setPrdctdn(parser.nextText());
                                    break;
//                            }
                        }
                        break;
                    }
                    case (XmlPullParser.END_TAG): { //adds to new vehicle
                        if ("prd".equals(name)) {
//                            Log.i("prediction_object", prediction.toString());
                            predictionList.add(prediction);
                        }
                        break;
                    }

                }
            } catch (NullPointerException e) {
                Log.e("nullpointer_xml", e.getMessage());
            }
            eventType = parser.next();
        }
    }

}




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