Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 16th, 2012  |  syntax: None  |  size: 4.10 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Faster Way to parse data and populate array?
  2. public ArrayList<String> getStops(String URL) {
  3.     ArrayList<String> BusStop = new ArrayList<String>();
  4.     String HTML = DownloadText(URL);
  5.     String temp = null;
  6.     String temp2[] = new String[40];
  7.     Pattern p = Pattern.compile("<a class="ada".*</a>", Pattern.DOTALL);
  8.  
  9.     Matcher m = p.matcher(HTML);
  10.     while (m.find()) {
  11.         temp = m.group();
  12.         temp2 = temp.split("<br></td>");
  13.     }
  14.  
  15.     for (int i = 0; i < temp2.length; i++) {
  16.         temp = temp2[i];
  17.         temp = temp.replaceAll("<a class="ada" title="", "");
  18.         temp = temp.replaceAll("".*"", "");
  19.         temp = temp.replaceAll("n", "");
  20.         temp = temp.replaceAll("t", "");
  21.         temp = temp.replaceAll(",</a>", "");
  22.         temp = temp.replaceAll("</tr>.*>", "");
  23.         temp = temp.replaceAll("<td.*>", "");
  24.         temp = temp.replaceAll(">.*", "");
  25.         BusStop.add(temp);
  26.     }
  27.  
  28.     return BusStop;
  29. }
  30.        
  31. TransitXMLExtractor extractor;
  32. static String baseURL5 = "http://www.ltconline.ca/webwatch/ada.aspx?r=1&d=2";
  33.  
  34. /** Populates string array with bus routes */
  35. public String[] busStopArray() {
  36.     extractor = new TransitXMLExtractor();
  37.     String[] busStopArray = new String[31];
  38.  
  39.     for (int n = 0; n < busStopArray.length; n++) {
  40.         busStopArray[n] = extractor.getStops(baseURL5).get(n);
  41.     }
  42.     return busStopArray;
  43.  
  44. }
  45.        
  46. public ArrayList<String> getStops(String URL) {
  47.     ArrayList<String> BusStop = new ArrayList<String>();
  48.     String HTML = DownloadText(URL);
  49.     Pattern p = Pattern.compile("<a class="ada" title="([\w\s]+)"");
  50.  
  51.     Matcher m = p.matcher(HTML);
  52.     while (m.find()) {
  53.         BusStop.add(m.group(1));
  54.     }
  55.  
  56.     return BusStop;
  57. }
  58.        
  59. public String[] busStopArray() {
  60.     extractor = new TransitXMLExtractor();
  61.  
  62.     return extractor.getStops(baseURL5).toArray(new String[0]);
  63. }
  64.        
  65. package com.kiswa.test;
  66.  
  67. import java.io.BufferedReader;
  68. import java.io.IOException;
  69. import java.io.InputStreamReader;
  70. import java.io.UnsupportedEncodingException;
  71. import java.net.URL;
  72. import java.util.ArrayList;
  73. import java.util.regex.Matcher;
  74. import java.util.regex.Pattern;
  75.  
  76. import android.app.Activity;
  77. import android.os.Bundle;
  78. import android.util.Log;
  79.  
  80. public class TestActivity extends Activity {
  81.     /** Called when the activity is first created. */
  82.     @Override
  83.     public void onCreate(Bundle savedInstanceState) {
  84.         super.onCreate(savedInstanceState);
  85.  
  86.         StringBuilder sb = new StringBuilder();
  87.         for (String stop : busStopArray()) {
  88.             sb.append(stop);
  89.         }
  90.         Log.d("STRING_TEST", sb.toString());
  91.  
  92.         setContentView(R.layout.main);
  93.     }
  94.  
  95.     public String DownloadText() throws UnsupportedEncodingException, IOException {
  96.         Log.d("STRING_TEST", "In DownloadText");
  97.         URL url = new URL("http://www.ltconline.ca/webwatch/ada.aspx?r=1&d=2");
  98.         BufferedReader reader = null;
  99.         StringBuilder builder = new StringBuilder();
  100.         try {
  101.             reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
  102.             for (String line; (line = reader.readLine()) != null;) {
  103.                 builder.append(line.trim());
  104.             }
  105.         } finally {
  106.             if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
  107.         }
  108.  
  109.         return builder.toString();
  110.     }
  111.  
  112.     public ArrayList<String> getStops() {
  113.         Log.d("STRING_TEST", "In getStops");
  114.         ArrayList<String> BusStop = new ArrayList<String>();
  115.         String HTML = "";
  116.         try {
  117.             HTML = DownloadText();
  118.         } catch (UnsupportedEncodingException e) {
  119.             // TODO Auto-generated catch block
  120.             e.printStackTrace();
  121.         } catch (IOException e) {
  122.             // TODO Auto-generated catch block
  123.             e.printStackTrace();
  124.         }
  125.         Pattern p = Pattern.compile("<a class="ada" title="([\w\s]+)"");
  126.  
  127.         Matcher m = p.matcher(HTML);
  128.         while (m.find()) {
  129.             BusStop.add(m.group(1));
  130.         }
  131.  
  132.         return BusStop;
  133.     }
  134.  
  135.     public String[] busStopArray() {
  136.         Log.d("STRING_TEST", "In busStopArray");
  137.         return getStops().toArray(new String[0]);
  138.     }
  139. }