I am working on a project, and while most of it works as needed, collecting the data is taking far too long. The problem is that I am making a lot of requests to the same server repeatedly, with different variables in the URL. So every time I try to fetch new data, I have to go through the handshaking process with the server, and it is slowing the program down considerably. So, is there anyway to keep the connection open while not having to constantly re-establish new connections? All I am changing are the stop and route variables in the URL. Below is the part of the code causing the issues.
Also if it helps, the project is pulling data from ctabustracker.com. I need the ETA of buses at all stops (not all but at least a few routes to show proof of concept), and as I said above it is taking far too long at the start of the program. Once all the initial data is pulled, much fewer requests will be made over any given period of time, but initially I have to pull data on every stop.
Any advice would be greatly appreciated.
Code:
private static void getTime(int stop, String route, buses theBus) throws InterruptedException {
int start, end, arrival;
long time;
String next;
String theURL = "http://ctabustracker.com/bustime/map/getStopPredictions.jsp?stop=";
theURL = theURL.concat(Integer.toString(stop)).concat("&route=").concat(route);
URL url = null;
URLConnection urlConn = null;
InputStreamReader inputStream = null;
BufferedReader buffer = null;
int eta1 = -1, eta2 = -1;
try {
url = new URL(theURL);
urlConn = url.openConnection();
inputStream = new InputStreamReader(urlConn.getInputStream());
buffer = new BufferedReader(inputStream);
for(int i = 0; i <= 2; i++){
next = buffer.readLine();
if(next == null)
break;
else {
if(next.contains(" MIN")) {
start = next.indexOf(">");
end = next.indexOf(" ", start);
arrival = Integer.parseInt(next.substring(start+1, end));
if(i == 0)
eta1 = arrival;
else
eta2 = arrival;
i++;
}
else
i--;
}
}
buffer.close();
theBus.eta(eta1, eta2, System.currentTimeMillis(), stop);
} catch(MalformedURLException e) {
System.out.println("Invalid URL:" + e.toString());
} catch(IOException e1) {
System.out.println("Can not contact URL:" + e1.toString());
}
}