diff --git a/src/edu/grinnell/glimmer/ushahidi/UshahidiRequestTask.java b/src/edu/grinnell/glimmer/ushahidi/UshahidiRequestTask.java new file mode 100644 index 0000000..d81559f --- /dev/null +++ b/src/edu/grinnell/glimmer/ushahidi/UshahidiRequestTask.java @@ -0,0 +1,65 @@ +package edu.grinnell.glimmer.ushahidi; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.json.JSONObject; + +import android.os.AsyncTask; + +public class UshahidiRequestTask extends AsyncTask{ + + + protected String doInBackground(String... input) { + try { + return makeRequest(input[0]); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + String makeRequest(String inputURL) throws Exception{ + URL serverURL = new URL(inputURL); + + // Connect to the server + HttpURLConnection connection; + BufferedReader input; // Textual input from the server + String line; // One line of data from the server + String text = ""; // Data read from the server + String data = ""; //Final result to return + + try { + connection = (HttpURLConnection) serverURL.openConnection(); + connection.connect(); + + // Read the data from the server + try { + input = new BufferedReader(new InputStreamReader( + connection.getInputStream())); + } catch (Exception e) { + throw new Exception("Could not get Ushahidi data from server."); + } + + while ((line = input.readLine()) != null) { + text += line; + } // while + + // Testing: What does the text look like? + // System.out.println(text); + + // Grab the JSON text, which starts with an open brace + text = text.substring(text.indexOf("{")); + + } catch (Exception e) { + throw new Exception("Could not connect to server because " + + e.toString()); + } + + return text; + + } + +} \ No newline at end of file diff --git a/src/edu/grinnell/glimmer/ushahidi/UshahidiWebClient.java b/src/edu/grinnell/glimmer/ushahidi/UshahidiWebClient.java index fe58ddb..1c5e685 100644 --- a/src/edu/grinnell/glimmer/ushahidi/UshahidiWebClient.java +++ b/src/edu/grinnell/glimmer/ushahidi/UshahidiWebClient.java @@ -15,7 +15,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package edu.grinnell.glimmer.ushahidi; import java.io.BufferedReader; @@ -144,6 +143,7 @@ public UshahidiWebClient(String server, int numIncidents) throws Exception { * (It will be fewer than numIncidents if the server provides fewer.) */ public int fetchIncidents() throws Exception { + String finalURL; BufferedReader input; // Textual input from the server String line; // One line of data from the server String text = ""; // Data read from the server @@ -157,39 +157,16 @@ public int fetchIncidents() throws Exception { if (this.maxId == 0) { serverURL = new URL(this.server + "/api?task=incidents&by=all&limit=" + numIncidents); + finalURL = this.server + "/api?task=incidents&by=all&limit=" + numIncidents; } else { serverURL = new URL(this.server + "/api?task=incidents&by=sinceid" + "&id=" + this.maxId + "&limit=" + numIncidents); + finalURL = this.server + "/api?task=incidents&by=sinceid" + + "&id=" + this.maxId + "&limit=" + numIncidents; } - // Connect to the server - HttpURLConnection connection; - try { - connection = (HttpURLConnection) serverURL.openConnection(); - connection.connect(); - } catch (Exception e) { - throw new Exception("Could not connect to " + this.server - + " because " + e.toString()); - } - - // Read the data from the server - try { - input = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - } catch (Exception e) { - throw new Exception("Could not get Ushahidi data from " - + this.server); - } - - while ((line = input.readLine()) != null) { - text += line; - } // while - - // Testing: What does the text look like? - // System.out.println(text); - - // Grab the JSON text, which starts with an open brace - text = text.substring(text.indexOf("{")); + //Get JSON text from an asyncronous call to the sercver + text = new UshahidiRequestTask().execute(finalURL).get(); // Convert the text to a JSONObject data = new JSONObject(text);