- Faster Way to parse data and populate array?
- public ArrayList<String> getStops(String URL) {
- ArrayList<String> BusStop = new ArrayList<String>();
- String HTML = DownloadText(URL);
- String temp = null;
- String temp2[] = new String[40];
- Pattern p = Pattern.compile("<a class="ada".*</a>", Pattern.DOTALL);
- Matcher m = p.matcher(HTML);
- while (m.find()) {
- temp = m.group();
- temp2 = temp.split("<br></td>");
- }
- for (int i = 0; i < temp2.length; i++) {
- temp = temp2[i];
- temp = temp.replaceAll("<a class="ada" title="", "");
- temp = temp.replaceAll("".*"", "");
- temp = temp.replaceAll("n", "");
- temp = temp.replaceAll("t", "");
- temp = temp.replaceAll(",</a>", "");
- temp = temp.replaceAll("</tr>.*>", "");
- temp = temp.replaceAll("<td.*>", "");
- temp = temp.replaceAll(">.*", "");
- BusStop.add(temp);
- }
- return BusStop;
- }
- TransitXMLExtractor extractor;
- static String baseURL5 = "http://www.ltconline.ca/webwatch/ada.aspx?r=1&d=2";
- /** Populates string array with bus routes */
- public String[] busStopArray() {
- extractor = new TransitXMLExtractor();
- String[] busStopArray = new String[31];
- for (int n = 0; n < busStopArray.length; n++) {
- busStopArray[n] = extractor.getStops(baseURL5).get(n);
- }
- return busStopArray;
- }
- public ArrayList<String> getStops(String URL) {
- ArrayList<String> BusStop = new ArrayList<String>();
- String HTML = DownloadText(URL);
- Pattern p = Pattern.compile("<a class="ada" title="([\w\s]+)"");
- Matcher m = p.matcher(HTML);
- while (m.find()) {
- BusStop.add(m.group(1));
- }
- return BusStop;
- }
- public String[] busStopArray() {
- extractor = new TransitXMLExtractor();
- return extractor.getStops(baseURL5).toArray(new String[0]);
- }
- package com.kiswa.test;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- public class TestActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- StringBuilder sb = new StringBuilder();
- for (String stop : busStopArray()) {
- sb.append(stop);
- }
- Log.d("STRING_TEST", sb.toString());
- setContentView(R.layout.main);
- }
- public String DownloadText() throws UnsupportedEncodingException, IOException {
- Log.d("STRING_TEST", "In DownloadText");
- URL url = new URL("http://www.ltconline.ca/webwatch/ada.aspx?r=1&d=2");
- BufferedReader reader = null;
- StringBuilder builder = new StringBuilder();
- try {
- reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
- for (String line; (line = reader.readLine()) != null;) {
- builder.append(line.trim());
- }
- } finally {
- if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
- }
- return builder.toString();
- }
- public ArrayList<String> getStops() {
- Log.d("STRING_TEST", "In getStops");
- ArrayList<String> BusStop = new ArrayList<String>();
- String HTML = "";
- try {
- HTML = DownloadText();
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Pattern p = Pattern.compile("<a class="ada" title="([\w\s]+)"");
- Matcher m = p.matcher(HTML);
- while (m.find()) {
- BusStop.add(m.group(1));
- }
- return BusStop;
- }
- public String[] busStopArray() {
- Log.d("STRING_TEST", "In busStopArray");
- return getStops().toArray(new String[0]);
- }
- }