Android Open Source - BusFollower O C Transpo Data Fetcher






From Project

Back to project page BusFollower.

License

The source code is released under:

GNU General Public License

If you think the Android project BusFollower 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

/*
 * Copyright 2012 Clayton Smith//from w w  w.  j a  v a  2s  . c o  m
 *
 * This file is part of Ottawa Bus Follower.
 *
 * Ottawa Bus Follower is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3, or (at
 * your option) any later version.
 *
 * Ottawa Bus Follower is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ottawa Bus Follower; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.argilo.busfollower.ocdata;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import net.argilo.busfollower.R;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class OCTranspoDataFetcher {
    private static final int TIMEOUT_CONNECTION = 15000;
    private static final int TIMEOUT_SOCKET = 15000;
    
    private Context context;
    private SQLiteDatabase db;
    private HttpClient httpClient = null;
    
    public OCTranspoDataFetcher(Context context, SQLiteDatabase db) {
        this.context = context;
        this.db = db;
    }
    
    public GetNextTripsForStopResult getNextTripsForStop(String stopNumber, String routeNumber)
            throws IOException, XmlPullParserException, IllegalArgumentException {
        validateStopNumber(stopNumber);
        validateRouteNumber(routeNumber);
        
        httpClient = new DefaultHttpClient(getHttpParams());
        HttpPost post = new HttpPost("https://api.octranspo1.com/v1.1/GetNextTripsForStop");
        
        List<NameValuePair> params = new ArrayList<NameValuePair>(4);
        params.add(new BasicNameValuePair("appID", context.getString(R.string.oc_transpo_application_id)));
        params.add(new BasicNameValuePair("apiKey", context.getString(R.string.oc_transpo_application_key)));
        params.add(new BasicNameValuePair("routeNo", routeNumber));
        params.add(new BasicNameValuePair("stopNo", stopNumber));
        post.setEntity(new UrlEncodedFormEntity(params));
        
        HttpResponse response = httpClient.execute(post);
        
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        InputStream in = response.getEntity().getContent();
        xpp.setInput(in, "UTF-8");
        xpp.next(); // <soap:Envelope>
        xpp.next(); //   <soap:Body>
        xpp.next(); //     <GetRouteSummaryForStopResponse>
        xpp.next(); //       <GetRouteSummaryForStopResult>
        GetNextTripsForStopResult result = new GetNextTripsForStopResult(context, db, xpp, stopNumber);
        in.close();
        return result;
    }
    
    public GetRouteSummaryForStopResult getRouteSummaryForStop(String stopNumber) throws IOException, XmlPullParserException {
        validateStopNumber(stopNumber);

        httpClient = new DefaultHttpClient(getHttpParams());
        HttpPost post = new HttpPost("https://api.octranspo1.com/v1.1/GetRouteSummaryForStop");
        
        List<NameValuePair> params = new ArrayList<NameValuePair>(3);
        params.add(new BasicNameValuePair("appID", context.getString(R.string.oc_transpo_application_id)));
        params.add(new BasicNameValuePair("apiKey", context.getString(R.string.oc_transpo_application_key)));
        params.add(new BasicNameValuePair("stopNo", stopNumber));
        post.setEntity(new UrlEncodedFormEntity(params));
        
        HttpResponse response = httpClient.execute(post);
        
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        InputStream in = response.getEntity().getContent();
        xpp.setInput(in, "UTF-8");
        xpp.next(); // <soap:Envelope>
        xpp.next(); //   <soap:Body>
        xpp.next(); //     <GetRouteSummaryForStopResponse>
        xpp.next(); //       <GetRouteSummaryForStopResult>
        GetRouteSummaryForStopResult result = new GetRouteSummaryForStopResult(xpp);
        in.close();
        return result;
    }
    
    public void abortRequest() {
        if (httpClient != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // This must not be done on the main thread.
                    httpClient.getConnectionManager().shutdown();
                }
            }).start();
        }
    }
    
    private HttpParams getHttpParams() {
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_CONNECTION);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_SOCKET);
        return httpParams;
    }
    
    private void validateStopNumber(String stopNumber) {
        if (stopNumber.length() < 3 || stopNumber.length() > 4) {
            throw new IllegalArgumentException(context.getString(R.string.invalid_stop_number));
        }
        for (int i = 0; i < stopNumber.length(); i++) {
            if (!Character.isDigit(stopNumber.charAt(i))) {
                throw new IllegalArgumentException(context.getString(R.string.invalid_stop_number));
            }
        }
    }
    
    private void validateRouteNumber(String routeNumber) {
        if (routeNumber.length() < 1 || routeNumber.length() > 3) {
            throw new IllegalArgumentException(context.getString(R.string.invalid_route_number));
        }
        for (int i = 0; i < routeNumber.length(); i++) {
            if (!Character.isDigit(routeNumber.charAt(i))) {
                throw new IllegalArgumentException(context.getString(R.string.invalid_route_number));
            }
        }
    }
}




Java Source Code List

net.argilo.busfollower.BusFollowerActivity.java
net.argilo.busfollower.BusFollowerApplication.java
net.argilo.busfollower.BusFollowerItemizedOverlay.java
net.argilo.busfollower.BusOverlayItem.java
net.argilo.busfollower.DoubleTapZoomMapView.java
net.argilo.busfollower.FetchRoutesTask.java
net.argilo.busfollower.FetchTripsTask.java
net.argilo.busfollower.MapChooserActivity.java
net.argilo.busfollower.RecentQueryList.java
net.argilo.busfollower.RecentQuery.java
net.argilo.busfollower.RouteChooserActivity.java
net.argilo.busfollower.StopChooserActivity.java
net.argilo.busfollower.StopOverlayItem.java
net.argilo.busfollower.StopsMapView.java
net.argilo.busfollower.Util.java
net.argilo.busfollower.ocdata.BusType.java
net.argilo.busfollower.ocdata.DatabaseHelper.java
net.argilo.busfollower.ocdata.GetNextTripsForStopResult.java
net.argilo.busfollower.ocdata.GetRouteSummaryForStopResult.java
net.argilo.busfollower.ocdata.OCTranspoDataFetcher.java
net.argilo.busfollower.ocdata.RouteDirection.java
net.argilo.busfollower.ocdata.Route.java
net.argilo.busfollower.ocdata.Stop.java
net.argilo.busfollower.ocdata.Trip.java
net.argilo.busfollower.ocdata.Util.java