Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/edu/grinnell/glimmer/ushahidi/UshahidiRequestTask.java
Original file line number Diff line number Diff line change
@@ -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<String, String, String>{


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;

}

}
35 changes: 6 additions & 29 deletions src/edu/grinnell/glimmer/ushahidi/UshahidiWebClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package edu.grinnell.glimmer.ushahidi;

import java.io.BufferedReader;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down