diff options
author | Christopher Baines <cbaines8@gmail.com> | 2012-03-27 19:58:50 +0100 |
---|---|---|
committer | Christopher Baines <cbaines8@gmail.com> | 2012-03-27 19:58:50 +0100 |
commit | ebfee560786ea52709696b763d5add619c9b33e8 (patch) | |
tree | c898f7573ba76bf38f1ff1c9bda63dbd0ec2da2f /src/net/cbaines/suca/SotonCalendarFetcher.java | |
parent | eb443719666878cedb73c1ab1f7987d5efe66af0 (diff) | |
download | southampton-university-calendar-applet-ebfee560786ea52709696b763d5add619c9b33e8.tar southampton-university-calendar-applet-ebfee560786ea52709696b763d5add619c9b33e8.tar.gz |
New interface, beginings of trying to make it work as an applet... :(
Diffstat (limited to 'src/net/cbaines/suca/SotonCalendarFetcher.java')
-rw-r--r-- | src/net/cbaines/suca/SotonCalendarFetcher.java | 57 |
1 files changed, 30 insertions, 27 deletions
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<NameValuePair> nvps = new ArrayList<NameValuePair>(); 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<NameValuePair>(); 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 |