Skip to content
Browse files

Code cleanup, and also making it easier to check connectivity simply …

…by changing a couple variables slightly.
  • Loading branch information...
1 parent 5a134c4 commit d129a367b1a762ab7a5c9fecb00f1c212bc7a49e @FDeSousa committed Jan 29, 2012
View
5 src/com/fdesousa/android/WheresMyTrain/Library/json/TflJsonFetcher.java
@@ -32,6 +32,7 @@
import android.net.NetworkInfo;
import android.util.Log;
+import com.fdesousa.android.WheresMyTrain.WheresMyTrain;
import com.fdesousa.android.WheresMyTrain.Library.LibraryMain;
/**
@@ -75,7 +76,7 @@ public static boolean isReachable(Context context) {
if (netInfo != null && netInfo.isConnected()) {
// Some sort of connection is open, check if server is reachable
try {
- URL url = new URL(TflJsonReader.BASE_URL);
+ URL url = new URL(TflJsonReader.URL_HOST);
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application");
urlc.setRequestProperty("Connection", "close");
@@ -89,7 +90,7 @@ public static boolean isReachable(Context context) {
}
} catch (IOException e) {
LibraryMain.displayToast("Could not reach server");
- Log.e("TflJsonFetcher", e.getMessage());
+ Log.e(WheresMyTrain.TAG + "TflJsonFetcher", e.getMessage());
return false;
}
} else {
View
6 src/com/fdesousa/android/WheresMyTrain/Library/json/TflJsonReader.java
@@ -48,7 +48,9 @@
/** URL divider of URL arguments, to be used after Query marker */
private static final char ARG_DIV = '&';
/** The base URL used for all TfL requests */
- protected static final String BASE_URL = "http://trains.desousa.com.pt/tfl.php";
+ protected static final String URL_HOST = "http://trains.desousa.com.pt";
+ /* The relative path to the PHP script to send requests to */
+ private static final String URL_PATH = "/tfl.php";
/** The base for the request type URL argument */
private static final String REQUEST_ARG = "request=";
/** The base for the line code URL argument */
@@ -99,7 +101,7 @@ private URI makeUri(final String request, final String line, final String statio
// If incidentsOnly is true, add it
if (incidentsOnly) arguments += ARG_DIV + INCIDENTS_ARG + PHP_TRUE_VALUE;
// Now make the URI with base and query elements
- return new URI(BASE_URL + QUERY + arguments);
+ return new URI(URL_HOST + URL_PATH + QUERY + arguments);
} catch (URISyntaxException e) {
Log.e(LibraryMain.TAG, e.getMessage());
LibraryMain.displayToast("Entered URL is not valid");
View
32 src/com/fdesousa/android/WheresMyTrain/WheresMyTrain.java
@@ -28,6 +28,7 @@
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
+import android.widget.Toast;
import com.fdesousa.android.WheresMyTrain.Library.LibraryMain;
import com.fdesousa.android.WheresMyTrain.Library.json.TflJsonFetcher;
@@ -45,7 +46,6 @@
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;
-@SuppressWarnings("rawtypes")
/**
* <b>WheresMyTrain : Activity</b>
* <p>Main Activity for the app, instantiating and controlling most UI elements.<br/>
@@ -56,7 +56,7 @@
public class WheresMyTrain extends ExpandableListActivity {
// Useful logging variables
/** Tag to be used when Logging an exception/error/anything at all */
- public static final String TAG = "WheresMyTrain";
+ public static final String TAG = "com.fdesousa.android.WheresMyTrain";
/**
* Current instance of this activity. Quick and dirty access to resources,
* context, etc.
@@ -125,10 +125,20 @@ public void onCreate(Bundle savedInstanceState) {
// Give LibraryMain a Context instance
LibraryMain.setContext(this.getBaseContext());
// Check the connectivity
+ checkConnectivity();
+ if (this.connected) {
+ // Now it's time to instantiate and setup things
+ instantiateVariables();
+ setupWidgets();
+ }
+ }
+
+ private void checkConnectivity() {
this.connected = TflJsonFetcher.isReachable(this);
- // Now it's time to instantiate and setup things
- instantiateVariables();
- setupWidgets();
+
+ if (!this.connected) {
+ Toast.makeText(this, "Server is unreachable. Check connectivity", Toast.LENGTH_LONG);
+ }
}
@Override
@@ -288,7 +298,7 @@ private void setupStationsSpinner() {
// Fetch, parse, display the list of lines and stations
/** To avoid conflicts, have a copy of the AsyncTask to cancel if needed */
- private AsyncTask prepareStationsList;
+ private AsyncTask<Void, Void, SLContainer> prepareStationsList;
/**
* AsyncTask sub-class to achieve a non-blocking manner in which to get the
@@ -301,8 +311,7 @@ private void setupStationsSpinner() {
*
* @author Filipe De Sousa
*/
- private class PrepareStationsList extends
- AsyncTask<Void, Void, SLContainer> {
+ private class PrepareStationsList extends AsyncTask<Void, Void, SLContainer> {
@Override
protected SLContainer doInBackground(Void... params) {
// Send the request to prepare the JSON data while other stuff goes on
@@ -321,7 +330,7 @@ protected void onPostExecute(SLContainer result) {
// Get/Refresh the detailed predictions
/** To avoid conflicts, have a copy of the AsyncTask to cancel if needed */
- private AsyncTask getPredictions;
+ private AsyncTask<Void, Void, DPContainer> getPredictions;
/**
* AsyncTask sub-class to achieve a non-blocking manner in which to get
@@ -354,8 +363,7 @@ protected DPContainer doInBackground(Void... params) {
protected void onPostExecute(DPContainer result) {
// Because of how tfl.php sends predictions data, there is only ever
// ONE station in stations array
- mPlatformAdapter = new PlatformsExpListAdapter(
- result.stations.get(0).platforms);
+ mPlatformAdapter = new PlatformsExpListAdapter(result.stations.get(0).platforms);
// Show the expandable list view, to show new predictions
predictionsList.setVisibility(View.VISIBLE);
// (Re)set the adapter onto the ExpandableListView
@@ -370,7 +378,7 @@ protected void onPostExecute(DPContainer result) {
// Get/Refresh the line status
/** To avoid conflicts, have a copy of the AsyncTask to cancel if needed */
- private AsyncTask getLineStatus;
+ private AsyncTask<Void, Void, LSContainer> getLineStatus;
/**
* AsyncTask sub-class to achieve a non-blocking manner in which to get line

0 comments on commit d129a36

Please sign in to comment.
Something went wrong with that request. Please try again.