Quote:
Originally Posted by jasongb
Thanks!
I'll work with the JSON syntax!
|
I generally find JSON data easier to work with.
If you're using Java I have a simple class to work with the FIRST Alliance APIs.
If you use this and don't use AppEngine, then you need to replace the JSON imports with JSON classes found elsewhere.
Code:
package org.team3309.scout;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
public class FirstAlliance {
private static final String API = "http://www.thefirstalliance.org/api/api.json.php?action=";
public static Event[] getEvents(String teamNumber) {
Event[] competitions = new Event[0];
String url = API + "team-events&team-number=3309";
JSONArray events = getArray(url);
competitions = new Event[events.length()];
try {
for (int i = 0; i < events.length(); i++) {
JSONObject event = events.getJSONObject(i);
competitions[i] = new Event(event);
}
} catch (JSONException ex) {
ex.printStackTrace();
}
return competitions;
}
public static Event getEventById(int id){
String url = API+ "event-details&event-id="+id;
JSONObject json = getJson(url);
return new Event(json);
}
private static JSONArray getArray(String url) {
try {
HttpURLConnection c = (HttpURLConnection) new URL(url)
.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
c.getInputStream()));
String line;
String input = "";
while ((line = reader.readLine()) != null)
input += line;
JSONObject json = new JSONObject(input);
return json.getJSONArray("data");
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private static JSONObject getJson(String url){
try {
HttpURLConnection c = (HttpURLConnection) new URL(url)
.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
c.getInputStream()));
String line;
String input = "";
while ((line = reader.readLine()) != null)
input += line;
JSONObject json = new JSONObject(input);
return json.getJSONObject("data");
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static class Event{
private String name;
private int id;
private JSONArray rankingsJson;
private ArrayList<Ranking> rankings;
private Event(JSONObject json){
this.name = json.optString("name");
this.id = json.optInt("id");
this.rankingsJson = json.optJSONArray("rankings");
}
public String getName(){
return name;
}
public int getId(){
return id;
}
public ArrayList<Ranking> getRankings(){
if(rankings != null)
return rankings;
ArrayList<Ranking> list = new ArrayList<Ranking>();
for(int i=0; i<rankingsJson.length(); i++){
JSONObject json = rankingsJson.optJSONObject(i);
list.add(new Ranking(json.optString("Team"), json.optInt("Rank"), json.optString("Record (W-L-T)")));
}
return list;
}
public String toString(){
return name;
}
}
public static class Ranking{
private String team, record;
private int ranking;
private Ranking(String team, int ranking, String record){
this.team = team;
this.ranking = ranking;
this.record = record;
}
public String getRecord(){
return record;
}
public int getWins(){
return Integer.parseInt(record.substring(0, record.indexOf("-")));
}
public int getLosses(){
return Integer.parseInt(record.substring(record.indexOf("-")+1, record.indexOf("-", record.indexOf("-")+1)));
}
public int getTies(){
return Integer.parseInt(record.substring(record.lastIndexOf("-")+1));
}
public String getTeam(){
return team;
}
public int getRank(){
return ranking;
}
}
}