Android Open Source - BART Request Task






From Project

Back to project page BART.

License

The source code is released under:

GNU General Public License

If you think the Android project BART 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 (C) 2012  David Brodsky//from  w w  w  .ja v  a  2s  .  c om
 *  This file is part of Open BART.
 *
 *  Open BART 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 of the License, or
 *  (at your option) any later version.
 *
 *  Open BART 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 Open BART.  If not, see <http://www.gnu.org/licenses/>.
*/


package pro.dbro.bart;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

public class RequestTask extends AsyncTask<String, String, String> {
  //allow data passing to calling activity
  String request;
  boolean updateUI;
  
  RequestTask(String request, boolean updateUI) {
    super();
    this.request = request;
        this.updateUI = updateUI;
    }
  @Override
  protected String doInBackground(String... uri) {
    if(BART.USE_LOCAL_RESPONSES){
      Log.d("RequestTask","Using local responses");
      try {
        String filename = null;
        if (request.compareTo("etd") == 0)
          filename = "problem_etd.aspx.xml";
        else
          filename = "problem_sched.aspx.xml";
        
         BufferedReader r = new BufferedReader(new InputStreamReader((TheActivity.c.getAssets().open(filename))));
         StringBuilder total = new StringBuilder();
         String line;
         while ((line = r.readLine()) != null) {
             total.append(line);
         }
         return total.toString();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    
    HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
          String url = uri[0];
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
          return "error";
        } catch (IOException e) {
          Log.v("RequestTask", e.toString());
          return "error";
            //TODO Handle problems..
        }
        return responseString;
  }
  
  @Override
    protected void onPostExecute(String result) {
    sendMessage(result);
        super.onPostExecute(result);
    }
  
  private void sendMessage(String result) { // 0 = service stopped , 1 = service started, 2 = refresh view with call to bartApiRequest(), 3 = 
        int status = 3; // hardcode status for calling TheActivity.parseBart
      //Log.d("BART_Response", result);
        Intent intent = new Intent("service_status_change");
        // You can also include some extra data.
        intent.putExtra("status", status);
        intent.putExtra("result", result);
        intent.putExtra("request", request);
        intent.putExtra("updateUI", updateUI);
        LocalBroadcastManager.getInstance(TheActivity.c).sendBroadcast(intent);
      }
}




Java Source Code List

pro.dbro.bart.BART.java
pro.dbro.bart.BartLinearLayout.java
pro.dbro.bart.BartRouteParser.java
pro.dbro.bart.BartStationEtdParser.java
pro.dbro.bart.DeviceLocation.java
pro.dbro.bart.LocalPersistence.java
pro.dbro.bart.MapActivity.java
pro.dbro.bart.RequestTask.java
pro.dbro.bart.StationSuggestion.java
pro.dbro.bart.TextPlusIconArrayAdapter.java
pro.dbro.bart.TheActivity.java
pro.dbro.bart.UsherService.java
pro.dbro.bart.ViewCountDownTimer.java
pro.dbro.bart.etdResponse.java
pro.dbro.bart.etd.java
pro.dbro.bart.leg.java
pro.dbro.bart.routeResponse.java
pro.dbro.bart.route.java