aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2012-01-30 14:51:29 +0000
committerChristopher Baines <cbaines8@gmail.com>2012-01-30 14:51:29 +0000
commit7d6d2399e3e750a215f191c2f0d77319e3eb46f2 (patch)
treebe5928dc8d36cacfb96592ab6c4ecef041e84368
parente78d1fa40d32a97018e2831fd0592d6b6c1b3d7e (diff)
downloadsouthamptonuniversitymap-7d6d2399e3e750a215f191c2f0d77319e3eb46f2.tar
southamptonuniversitymap-7d6d2399e3e750a215f191c2f0d77319e3eb46f2.tar.gz
More data for users regarding errors when fetching timetables.
-rw-r--r--src/net/cbaines/suma/BusStopActivity.java18
-rw-r--r--src/net/cbaines/suma/DataManager.java81
2 files changed, 54 insertions, 45 deletions
diff --git a/src/net/cbaines/suma/BusStopActivity.java b/src/net/cbaines/suma/BusStopActivity.java
index 699ec2d..d97baa4 100644
--- a/src/net/cbaines/suma/BusStopActivity.java
+++ b/src/net/cbaines/suma/BusStopActivity.java
@@ -19,10 +19,14 @@
package net.cbaines.suma;
+import java.io.IOException;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
+import org.apache.http.client.ClientProtocolException;
+import org.json.JSONException;
+
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@@ -267,6 +271,8 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
}
private class GetTimetableTask extends AsyncTask<String, Integer, Timetable> {
+ String errorMessage;
+
protected void onPreExecute() {
progBar.setVisibility(View.VISIBLE);
}
@@ -276,6 +282,16 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
try {
newTimetable = DataManager.getTimetable(instance, busStopID, true);
} catch (SQLException e) {
+ errorMessage = "Error message regarding SQL?";
+ e.printStackTrace();
+ } catch (ClientProtocolException e) {
+ errorMessage = "Insert error message here!";
+ e.printStackTrace();
+ } catch (IOException e) {
+ errorMessage = "Error fetching bus times from server, are you connected to the internet?";
+ e.printStackTrace();
+ } catch (JSONException e) {
+ errorMessage = "Error parsing bus times";
e.printStackTrace();
}
return newTimetable;
@@ -287,7 +303,7 @@ public class BusStopActivity extends OrmLiteBaseActivity<DatabaseHelper> impleme
Log.i(TAG, "Its null");
progBar.setVisibility(View.GONE);
- busStopMessage.setText("Error fetching bus times");
+ busStopMessage.setText(errorMessage);
busStopMessage.setVisibility(View.VISIBLE);
} else {
progBar.setVisibility(View.GONE);
diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java
index bc82b59..9fd08c1 100644
--- a/src/net/cbaines/suma/DataManager.java
+++ b/src/net/cbaines/suma/DataManager.java
@@ -37,6 +37,7 @@ import javax.xml.parsers.SAXParserFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
+import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
@@ -545,7 +546,8 @@ public class DataManager {
}
- public static Timetable getTimetable(Context context, String busStop, boolean onlyUniLink) throws SQLException {
+ public static Timetable getTimetable(Context context, String busStop, boolean onlyUniLink) throws SQLException, ClientProtocolException, IOException,
+ JSONException {
if (helper == null)
helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
@@ -558,42 +560,36 @@ public class DataManager {
String file = getFileFromServer(busStopUrl + busStop + ".json");
- try {
- JSONObject data = new JSONObject(file);
+ JSONObject data = new JSONObject(file);
- JSONArray stopsArray = data.getJSONArray("stops");
+ JSONArray stopsArray = data.getJSONArray("stops");
- Log.i(TAG, "Number of entries " + data.length());
+ Log.i(TAG, "Number of entries " + data.length());
- Log.i(TAG, "Stops: " + data.getJSONArray("stops"));
+ Log.i(TAG, "Stops: " + data.getJSONArray("stops"));
- for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) {
- JSONObject stopObj = stopsArray.getJSONObject(stopNum);
+ for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) {
+ JSONObject stopObj = stopsArray.getJSONObject(stopNum);
- if (onlyUniLink && !stopObj.getString("name").startsWith("U")) {
- continue;
- }
+ if (onlyUniLink && !stopObj.getString("name").startsWith("U")) {
+ continue;
+ }
- BusStop busStopObj = busStopDao.queryForId(busStop);
- if (busStopObj == null) {
- Log.e(TAG, "BusStopObj == null");
- }
+ BusStop busStopObj = busStopDao.queryForId(busStop);
+ if (busStopObj == null) {
+ Log.e(TAG, "BusStopObj == null");
+ }
- Stop stop = getStop(context, stopObj, busStopObj);
+ Stop stop = getStop(context, stopObj, busStopObj);
- if (stop == null) {
- Log.w(TAG, "Null stop, skiping");
- continue;
- }
+ if (stop == null) {
+ Log.w(TAG, "Null stop, skiping");
+ continue;
+ }
- Log.i(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " + stop.arivalTime);
+ Log.i(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " + stop.arivalTime);
- timetable.add(stop);
- }
- } catch (JSONException ex) {
- Log.e(TAG, "", ex);
- Log.e(TAG, "File: " + file);
- return null;
+ timetable.add(stop);
}
timetable.fetchTime = new Date(System.currentTimeMillis());
@@ -628,28 +624,25 @@ public class DataManager {
return data;
}
- public static String getFileFromServer(String request) {
+ public static String getFileFromServer(String request) throws ClientProtocolException, IOException {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(request);
Log.i("Util.getFileFromServer", "Request used: " + request);
- try {
- HttpResponse response = client.execute(httpGet);
- StatusLine statusLine = response.getStatusLine();
- int statusCode = statusLine.getStatusCode();
- if (statusCode == 200) {
- HttpEntity entity = response.getEntity();
- InputStream content = entity.getContent();
- BufferedReader reader = new BufferedReader(new InputStreamReader(content));
- String line;
- while ((line = reader.readLine()) != null) {
- builder.append(line);
- }
- } else {
- Log.e("", "Failed to download file");
+
+ HttpResponse response = client.execute(httpGet);
+ StatusLine statusLine = response.getStatusLine();
+ int statusCode = statusLine.getStatusCode();
+ if (statusCode == 200) {
+ HttpEntity entity = response.getEntity();
+ InputStream content = entity.getContent();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(content));
+ String line;
+ while ((line = reader.readLine()) != null) {
+ builder.append(line);
}
- } catch (Exception ex) {
- Log.e("Util.getFileFromServer", ex.getClass().toString() + " " + ex.getMessage());
+ } else {
+ Log.e("", "Failed to download file");
}
return builder.toString();