From ebfee560786ea52709696b763d5add619c9b33e8 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Tue, 27 Mar 2012 19:58:50 +0100 Subject: New interface, beginings of trying to make it work as an applet... :( --- src/net/cbaines/suca/SotonCalendarFetcher.java | 57 ++++++++++++++------------ 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'src/net/cbaines/suca/SotonCalendarFetcher.java') diff --git a/src/net/cbaines/suca/SotonCalendarFetcher.java b/src/net/cbaines/suca/SotonCalendarFetcher.java index 40a85ff..846faf1 100644 --- a/src/net/cbaines/suca/SotonCalendarFetcher.java +++ b/src/net/cbaines/suca/SotonCalendarFetcher.java @@ -30,11 +30,13 @@ public class SotonCalendarFetcher { this.password = password; } - public String fetchAndParseTimetable() throws LoginException { + @SuppressWarnings("deprecation") + public String fetchAndParseTimetable() throws IOException, LoginException { DefaultHttpClient httpclient = new DefaultHttpClient(); + Exception exception = null; + try { - HttpGet httpget = new HttpGet( - "https://www.adminservices.soton.ac.uk/adminweb/servlet/login"); + HttpGet httpget = new HttpGet("https://www.adminservices.soton.ac.uk/adminweb/servlet/login"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); @@ -43,11 +45,7 @@ public class SotonCalendarFetcher { // response.getStatusLine()); // EntityUtils.consume(entity); if (entity != null) { - try { - entity.consumeContent(); - } catch (IOException e) { - e.printStackTrace(); - } + entity.consumeContent(); } // System.out.println("Initial set of cookies:"); @@ -60,8 +58,7 @@ public class SotonCalendarFetcher { } } - HttpPost httpost = new HttpPost( - "https://www.adminservices.soton.ac.uk/adminweb/servlet/login"); + HttpPost httpost = new HttpPost("https://www.adminservices.soton.ac.uk/adminweb/servlet/login"); List nvps = new ArrayList(); nvps.add(new BasicNameValuePair("user", username)); @@ -81,11 +78,16 @@ public class SotonCalendarFetcher { // System.out.println("Login form get: " + // response.getStatusLine()); // EntityUtils.consume(entity); - BufferedReader br = new BufferedReader(new InputStreamReader( - entity.getContent())); - String line; - while ((line = br.readLine()) != null) { - // Log.v(TAG, line); + BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); + StringBuilder responseBuilder = new StringBuilder(); + for (String line; (line = br.readLine()) != null;) { + responseBuilder.append(line); + } + + System.out.println("responseBuilder.length() " + responseBuilder.length()); + + if (responseBuilder.length() != 0) { + throw new LoginException("Login failed"); } // System.out.println("Post logon cookies:"); @@ -117,6 +119,7 @@ public class SotonCalendarFetcher { System.out.println("Second post get: " + response.getStatusLine()); // EntityUtils.consume(entity); br = new BufferedReader(new InputStreamReader(entity.getContent())); + String line; while ((line = br.readLine()) != null) { // System.out.println(line); } @@ -144,8 +147,7 @@ public class SotonCalendarFetcher { // Log.v(TAG, line); } - httpost = new HttpPost( - "https://www.adminservices.soton.ac.uk/adminweb/timetables/ttdownload/.csv"); + httpost = new HttpPost("https://www.adminservices.soton.ac.uk/adminweb/timetables/ttdownload/.csv"); nvps = new ArrayList(); nvps.add(new BasicNameValuePair("format", "list")); @@ -157,19 +159,20 @@ public class SotonCalendarFetcher { String responseString = IOUtils.toString(entity.getContent()); - if (responseString.contains("Failed Login")) { - throw new LoginException("Login failed"); - } else { - return responseString; - } + return responseString; - } catch (IllegalStateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + exception = e; + } catch (LoginException e) { + exception = e; } finally { + if (exception != null) { + if (exception instanceof IOException) { + throw (IOException) exception; + } else if (exception instanceof IOException) { + throw (LoginException) exception; + } + } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources -- cgit v1.2.3