Example usage for com.google.gson JsonArray iterator

List of usage examples for com.google.gson JsonArray iterator

Introduction

In this page you can find the example usage for com.google.gson JsonArray iterator.

Prototype

public Iterator<JsonElement> iterator() 

Source Link

Document

Returns an iterator to navigate the elements of the array.

Usage

From source file:com.plnyyanks.frcnotebook.database.DatabaseHandler.java

License:Open Source License

public void importTeams(JsonArray teams) {
    Iterator<JsonElement> iterator = teams.iterator();
    JsonObject t;//from w ww  .  j a  v a2s . com
    Team team;

    while (iterator.hasNext()) {
        t = iterator.next().getAsJsonObject();
        team = new Team();

        team.setTeamKey(t.get(KEY_TEAMKEY).getAsString());
        try {
            team.setTeamName(t.get(KEY_TEAMNAME).getAsString());
        } catch (Exception e) {
            team.setTeamName("");
        }
        team.setTeamNumber(t.get(KEY_TEAMNUMBER).getAsInt());
        try {
            team.setTeamWebsite(t.get(KEY_TEAMSITE).getAsString());
        } catch (Exception e) {
            team.setTeamWebsite("");
        }

        team.setTeamEvents(JSONManager.getAsStringArrayList(t.get(KEY_TEAMEVENTS).getAsJsonArray().toString()));

        addTeam(team);
    }
}

From source file:com.plnyyanks.frcnotebook.database.DatabaseHandler.java

License:Open Source License

public void importNotes(JsonArray notes) {
    Iterator<JsonElement> iterator = notes.iterator();
    JsonObject n;//from  w w w . j a  v a2 s.com
    Note note;

    while (iterator.hasNext()) {
        n = iterator.next().getAsJsonObject();
        note = new Note();

        note.setId(n.get(KEY_NOTEID).getAsShort());
        note.setEventKey(n.get(KEY_EVENTKEY).getAsString());
        note.setMatchKey(n.get(KEY_MATCHKEY).getAsString());
        note.setTeamKey(n.get(KEY_TEAMKEY).getAsString());
        note.setNote(n.get(KEY_NOTE).getAsString());
        note.setTimestamp(n.get(KEY_NOTETIME).getAsLong());
        try {
            note.setParent(n.get(KEY_NOTEPARENT).getAsShort());
            note.setPictures(n.get(KEY_NOTEPICS).getAsString());
        } catch (Exception e) {
            note.setParent((short) -1);
            note.setPictures("");
        }

        addNote(note);
    }
}

From source file:com.plnyyanks.frcnotebook.database.DatabaseHandler.java

License:Open Source License

public void importDefNotes(JsonArray notes) {
    Iterator<JsonElement> iterator = notes.iterator();
    JsonObject n;/*  w w w .  ja  v  a2 s  .  c  o  m*/

    while (iterator.hasNext()) {
        n = iterator.next().getAsJsonObject();

        addDefNote(n.get(KEY_DEF_NOTE).getAsString());
    }
}

From source file:com.plnyyanks.frcnotebook.datafeed.TBADatafeed.java

License:Open Source License

public static String fetchEventDetails_TBAv1(String eventKey) {
    String data = GET_Request
            .getWebData("http://www.thebluealliance.com/api/v1/event/details?event=" + eventKey, true);

    JsonObject eventObject = JSONManager.getAsJsonObject(data);
    JsonArray teams = eventObject.getAsJsonArray("teams");
    JsonArray matches = eventObject.getAsJsonArray("matches");

    Event event = new Event();

    event.setEventKey(eventKey);/* ww  w . j  a v  a2  s . c om*/

    String name = eventObject.get("name").getAsString();
    event.setEventName(name);

    String shortName = eventObject.get("short_name").getAsString();
    event.setShortName(shortName);

    String year = eventObject.get("year").getAsString();
    event.setEventYear(Integer.parseInt(year));

    String location = eventObject.get("location").getAsString();
    event.setEventLocation(location);

    String start = eventObject.get("start_date").getAsString();
    event.setEventStart(start);

    String end = eventObject.get("end_date").getAsString();
    event.setEventEnd(end);

    Log.d(Constants.LOG_TAG, "official: " + eventObject.get("official"));
    event.setOfficial(eventObject.get("official").getAsBoolean());

    long eventResult = StartActivity.db.addEvent(event);

    if (eventResult == -1) {
        return "Error writing event to database";
    }

    Iterator<JsonElement> iterator = teams.iterator();
    JsonElement teamElement;
    Team team;

    long teamResult = 0;
    while (iterator.hasNext() && teamResult != -1) {
        teamElement = iterator.next();
        team = new Team();
        team.setTeamKey(teamElement.getAsString());
        team.setTeamNumber(Integer.parseInt(team.getTeamKey().substring(3)));
        team.addEvent(eventKey);

        teamResult = StartActivity.db.addTeam(team);
    }

    if (teamResult == -1) {
        return "Error writing teams to database";
    } else {
        if (matches.size() > 0) {
            TBADatafeed.fetchMatches_TBAv1(matches.toString());
        }

        return "Info downloaded for " + eventKey;
    }

}

From source file:com.plnyyanks.frcnotebook.datafeed.TBADatafeed.java

License:Open Source License

public static String fetchEventDetails_TBAv2(String eventKey) {
    String eventData = GET_Request.getWebData("http://www.thebluealliance.com/api/v2/event/" + eventKey, true);
    String teamData = GET_Request
            .getWebData("http://www.thebluealliance.com/api/v2/event/" + eventKey + "/teams", true);

    JsonObject eventObject = JSONManager.getAsJsonObject(eventData);
    JsonArray teams = JSONManager.getasJsonArray(teamData);

    Event event = new Event();
    event.setEventKey(eventKey);//from   w w  w.  ja v a 2  s .co  m
    event.setEventName(eventObject.get("name").getAsString());
    event.setShortName(eventObject.get("short_name").getAsString());
    event.setEventYear(eventObject.get("year").getAsInt());
    event.setEventLocation(eventObject.get("location").getAsString());
    event.setEventStart(eventObject.get("start_date").getAsString());
    event.setEventEnd(eventObject.get("end_date").getAsString());
    event.setOfficial(eventObject.get("official").getAsBoolean());

    long eventResult = StartActivity.db.addEvent(event);

    if (eventResult == -1) {
        return "Error writing event to database";
    }

    Iterator<JsonElement> iterator = teams.iterator();
    JsonObject teamObject;
    Team team;

    long teamResult = 0;
    while (iterator.hasNext() && teamResult != -1) {
        teamObject = iterator.next().getAsJsonObject();
        team = new Team();
        team.setTeamKey(teamObject.get("key").getAsString());
        team.setTeamNumber(teamObject.get("team_number").getAsInt());
        team.addEvent(eventKey);

        teamResult = StartActivity.db.addTeam(team);
    }

    if (teamResult == -1) {
        return "Error writing teams to database";
    } else {
        if (PreferenceHandler.getDataSource() == Constants.DATAFEED_SOURCES.TBAv2)
            fetchMatches_TBAv2(eventKey);
        else if (PreferenceHandler.getDataSource() == Constants.DATAFEED_SOURCES.USFIRST) {
            String matchResult = USFIRSTParser.fetchMatches_USFIRST(eventKey);
            if (!matchResult.isEmpty())
                return matchResult;
        }
    }
    return "Info downloaded for " + eventKey;
}

From source file:com.plnyyanks.frcnotebook.datafeed.TBADatafeed.java

License:Open Source License

public static LinkedHashMap<String, ListItem> fetchEvents_TBAv1(String year) {
    String data = GET_Request.getWebData("http://www.thebluealliance.com/api/v1/events/list?year=" + year,
            true);/*from  ww w .  j  a v  a 2s .c  om*/
    JsonArray result = JSONManager.getasJsonArray(data);

    //now, add the events to the event picker activity
    Iterator<JsonElement> iterator = result.iterator();

    JsonObject element;
    String eventName, eventKey;
    ArrayList<Event> list = new ArrayList<Event>();
    for (int i = 0; i < result.size() && iterator.hasNext(); i++) {
        element = iterator.next().getAsJsonObject();
        eventName = element.get("name").getAsString();
        eventKey = element.get("key").getAsString();

        Event e = new Event();
        e.setEventKey(eventKey);
        e.setEventName(eventName);
        e.setEventStart(element.get("start_date").getAsString());
        list.add(e);
    }

    Collections.sort(list);
    int eventWeek = Integer.parseInt(Event.weekFormatter.format(new Date())), currentWeek;
    LinkedHashMap<String, ListItem> output = new LinkedHashMap<String, ListItem>();
    for (Event e : list) {
        currentWeek = e.getCompetitionWeek();
        if (eventWeek != currentWeek) {
            String header;
            if (currentWeek == 9) {
                header = year + " Championship Event";
            } else {
                header = year + " Week " + currentWeek;
            }
            output.put("week" + currentWeek, new ListHeader(header));
        }
        eventWeek = currentWeek;

        output.put(e.getEventKey(), new ListElement(e.getEventName(), e.getEventKey()));
    }

    return output;
}

From source file:com.plnyyanks.frcnotebook.datafeed.TBADatafeed.java

License:Open Source License

public static void fetchMatches_TBAv1(String matchesToFetch) {
    String matchList = matchesToFetch.replaceAll("\"", "");
    matchList = matchList.substring(1, matchList.length() - 1);

    String requestString = "http://www.thebluealliance.com/api/v1/match/details?match=" + matchList;
    String result = GET_Request.getWebData(requestString, true);

    JsonArray matches = JSONManager.getasJsonArray(result);
    Iterator<JsonElement> iterator = matches.iterator();
    Match match;//from   w  w  w.  ja v  a 2s . com
    JsonObject element, alliances, red, blue;
    JsonArray redTeams, blueTeams;
    while (iterator.hasNext()) {
        //for each match we get data for...
        element = iterator.next().getAsJsonObject();
        match = new Match();
        match.setMatchKey(element.get("key").getAsString());
        match.setMatchType(Constants.MATCH_LEVELS.get(element.get("competition_level").getAsString()));
        match.setMatchNumber(Integer.parseInt(element.get("match_number").getAsString()));
        match.setSetNumber(Integer.parseInt(element.get("set_number").getAsString()));

        //now, for alliances. Hardest part of JSON wizardry...
        alliances = element.get("alliances").getAsJsonObject();
        red = alliances.get("red").getAsJsonObject();
        redTeams = red.get("teams").getAsJsonArray();
        blue = alliances.get("blue").getAsJsonObject();
        blueTeams = blue.get("teams").getAsJsonArray();

        match.setRedScore(Integer.parseInt(red.get("score").getAsString()));
        match.setBlueScore(Integer.parseInt(blue.get("score").getAsString()));

        match.setRedAlliance(redTeams.toString());
        match.setBlueAlliance(blueTeams.toString());
        StartActivity.db.addMatch(match);
    }
}

From source file:com.plnyyanks.frcnotebook.datafeed.TBADatafeed.java

License:Open Source License

public static void fetchMatches_TBAv2(String eventKey) {
    String requestString = "http://www.thebluealliance.com/api/v2/event/" + eventKey + "/matches";
    String result = GET_Request.getWebData(requestString, true);

    JsonArray matches = JSONManager.getasJsonArray(result);
    Iterator<JsonElement> iterator = matches.iterator();
    Match match;//w w w  . ja va2s .c  o m
    JsonObject element, alliances, red, blue;
    JsonArray redTeams, blueTeams;
    while (iterator.hasNext()) {
        //for each match we get data for...
        element = iterator.next().getAsJsonObject();
        match = new Match();
        match.setMatchKey(element.get("key").getAsString());
        match.setMatchType(Constants.MATCH_LEVELS.get(element.get("comp_level").getAsString()));
        match.setMatchNumber(element.get("match_number").getAsInt());
        match.setSetNumber(element.get("set_number").getAsInt());

        //now, for alliances. Hardest part of JSON wizardry...
        alliances = element.get("alliances").getAsJsonObject();
        red = alliances.get("red").getAsJsonObject();
        redTeams = red.get("teams").getAsJsonArray();
        blue = alliances.get("blue").getAsJsonObject();
        blueTeams = blue.get("teams").getAsJsonArray();

        match.setRedScore(red.get("score").getAsInt());
        match.setBlueScore(blue.get("score").getAsInt());

        match.setRedAlliance(redTeams.toString());
        match.setBlueAlliance(blueTeams.toString());
        StartActivity.db.addMatch(match);
    }
}

From source file:com.plnyyanks.frcnotebook.dialogs.AddNoteDialog.java

License:Open Source License

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    activity = this.getActivity();
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle(R.string.add_note_title);
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.fragment_add_note, null);

    ImageView camIcon = (ImageView) layout.findViewById(R.id.add_note_picture);
    camIcon.setOnClickListener(new AddPictureListener());
    if (PreferenceHandler.getTheme() == R.style.theme_dark) {
        camIcon.setBackgroundResource(R.drawable.ic_action_camera_dark);
    }/*from   w ww  .j a v a 2 s  .c  o m*/

    //if match is set, get the teams for this match
    final JsonArray redAlliance, blueAlliance;
    JsonArray teams;
    String[] team_choices;
    if (match != null) {
        redAlliance = match.getRedAllianceTeams();
        blueAlliance = match.getBlueAllianceTeams();
        teams = new JsonArray();
        teams.addAll(redAlliance);
        teams.addAll(blueAlliance);

        team_choices = new String[teams.size() + 1];
        team_choices[0] = getResources().getString(R.string.all_teams);
        for (int i = 1; i < team_choices.length; i++) {
            team_choices[i] = teams.get(i - 1).getAsString().substring(3);
        }
    } else {
        if (GetNotesForTeam.getTeamKey().equals("all")) {
            ArrayList<Team> all_teams = StartActivity.db.getAllTeamAtEvent(GetNotesForTeam.getEventKey());
            team_choices = new String[all_teams.size() + 1];
            team_choices[0] = activity.getString(R.string.all_teams);
            for (int i = 1; i < all_teams.size(); i++) {
                team_choices[i] = Integer.toString(all_teams.get(i - 1).getTeamNumber());
            }
        } else {
            //we're on the team page, so only allow this team
            team_choices = new String[1];
            team_choices[0] = GetNotesForTeam.getTeamNumber();
        }

        //keep the compiler happy
        redAlliance = new JsonArray();
        blueAlliance = new JsonArray();
    }

    //get predefined notes
    HashMap<Short, String> allPredefNotes = StartActivity.db.getAllDefNotes();
    final String[] note_choice = new String[allPredefNotes.size() + 1];
    final short[] note_choice_ids = new short[allPredefNotes.size() + 1];
    note_choice[0] = "Custom Note";
    note_choice_ids[0] = -1;
    Iterator<Short> iterator = allPredefNotes.keySet().iterator();
    Short key;
    for (int i = 1; i < note_choice.length && iterator.hasNext(); i++) {
        key = iterator.next();
        note_choice_ids[i] = key;
        note_choice[i] = allPredefNotes.get(key);
    }

    //get all the matches at this event
    final String[] match_choices;
    final String[] match_keys;
    if (match != null) {
        match_choices = new String[1];
        match_choices[0] = match.getTitle();

        match_keys = new String[1];
        match_keys[0] = match.getMatchKey();
    } else {
        ArrayList<Match> matches = new ArrayList<Match>();
        if (eventKey.equals("all")) {
            matches = StartActivity.db.getAllMatchesForTeam(GetNotesForTeam.getTeamKey());
        } else {
            matches = StartActivity.db.getAllMatches(eventKey, GetNotesForTeam.getTeamKey());
        }
        match_choices = new String[matches.size() + 1];
        match_keys = new String[matches.size() + 1];
        match_choices[0] = getString(R.string.generic_note);
        match_keys[0] = "all";
        for (int i = 1; i < match_choices.length; i++) {
            match_choices[i] = matches.get(i - 1).getTitle(eventKey.equals("all"));
            match_keys[i] = matches.get(i - 1).getMatchKey();
        }
    }

    final Spinner teamSpinner = (Spinner) layout.findViewById(R.id.team_selector);
    final Spinner noteSpinner = (Spinner) layout.findViewById(R.id.predef_note_selector);
    final Spinner matchSpinner = (Spinner) layout.findViewById(R.id.match_selector);
    final EditText e = (EditText) layout.findViewById(R.id.note_contents);
    noteSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            if (i == 0) {
                e.setVisibility(View.VISIBLE);
            } else {
                e.setVisibility(View.GONE);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
    final ArrayAdapter teamAdapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item,
            team_choices);
    teamAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    teamSpinner.setAdapter(teamAdapter);
    teamSpinner.setEnabled(match != null || GetNotesForTeam.getTeamKey().equals("all"));

    ArrayAdapter noteAdapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, note_choice);
    noteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    noteSpinner.setAdapter(noteAdapter);

    ArrayAdapter matchAdapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, match_choices);
    matchAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    matchSpinner.setAdapter(matchAdapter);
    matchSpinner.setEnabled(match == null);

    builder.setView(layout);

    // Add the buttons
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

            if (noteSpinner.getSelectedItemPosition() == 0 && e.getText().toString().isEmpty()) {
                dialog.cancel();
                return;
            }
            if (match != null) {
                Note newNote = new Note();
                newNote.setMatchKey(match_keys[matchSpinner.getSelectedItemPosition()]);
                newNote.setEventKey(match.getParentEvent().getEventKey());
                newNote.setParent(note_choice_ids[noteSpinner.getSelectedItemPosition()]);

                if (noteSpinner.getSelectedItemPosition() == 0) {
                    newNote.setNote(e.getText().toString());
                } else {
                    newNote.setNote(note_choice[noteSpinner.getSelectedItemPosition()]);
                }

                if (teamSpinner.getSelectedItem()
                        .equals(activity.getResources().getString(R.string.all_teams))) {
                    //add note for all teams
                    newNote.setTeamKey("all");
                    short newId = StartActivity.db.addNote(newNote);
                    if (newId == -1) {
                        dialog.cancel();
                        return;
                    }
                    if (GetNotesForMatch.getGenericAdapter().keys.size() == 0) {
                        ListView list = (ListView) activity.findViewById(R.id.generic_notes);
                        list.setVisibility(View.VISIBLE);
                    }
                    newNote = StartActivity.db.getNote(newId);
                    genericAdapter.addNote(newNote);

                } else {
                    //generate team key
                    String team = "frc" + (String) teamSpinner.getSelectedItem();
                    Iterator iterator = redAlliance.iterator();
                    String testTeam;
                    newNote.setTeamKey(team);
                    Log.d(Constants.LOG_TAG, "team key: " + newNote.getTeamKey());
                    while (iterator.hasNext()) {
                        testTeam = iterator.next().toString();
                        if (testTeam.equals("\"" + team + "\"")) {
                            short newId = StartActivity.db.addNote(newNote);
                            newNote = StartActivity.db.getNote(newId);
                            Log.d(Constants.LOG_TAG, "id: " + newId + " team: " + newNote.getTeamKey());
                            redAdapter.addNote(newNote);
                            dialog.cancel();
                            return;
                        }
                    }
                    iterator = blueAlliance.iterator();
                    while (iterator.hasNext()) {
                        testTeam = iterator.next().toString();
                        if (testTeam.equals("\"" + team + "\"")) {
                            short newId = StartActivity.db.addNote(newNote);
                            blueAdapter.addNote(StartActivity.db.getNote(newId));
                            ;
                            dialog.cancel();
                            return;
                        }
                    }
                }
            } else {
                //add the note on the team view activity
                Note newNote = new Note();
                newNote.setMatchKey(match_keys[matchSpinner.getSelectedItemPosition()]);
                if (newNote.getMatchKey().equals("all")) {
                    newNote.setEventKey(eventKey);
                } else {
                    newNote.setEventKey(newNote.getMatchKey().split("_")[0]);
                }
                newNote.setParent(note_choice_ids[noteSpinner.getSelectedItemPosition()]);
                String team = "frc" + (String) teamSpinner.getSelectedItem();
                newNote.setTeamKey(team.equals(activity.getString(R.string.all_teams)) ? "all" : team);

                if (noteSpinner.getSelectedItemPosition() == 0) {
                    //set note to custom text
                    newNote.setNote(e.getText().toString());
                } else {
                    //set note to a predefined note
                    newNote.setNote(note_choice[noteSpinner.getSelectedItemPosition()]);
                }

                short newId = StartActivity.db.addNote(newNote);
                if (newId == -1) {
                    dialog.cancel();
                    return;
                }
                newNote = StartActivity.db.getNote(newId);
                teamViewAdapter.addNote(newNote);
            }
        }
    });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog
            dialog.dismiss();
        }
    });

    Dialog out = builder.create();
    out.getWindow().clearFlags(
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    out.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    return out;
}

From source file:com.rackspacecloud.blueflood.outputs.handlers.HttpMultiRollupsQueryHandler.java

License:Apache License

private List<String> getLocatorsFromJSONBody(String tenantId, String body) {
    JsonElement element = gson.fromJson(body, JsonElement.class);
    JsonArray metrics = element.getAsJsonArray();
    final List<String> locators = new ArrayList<String>();

    Iterator<JsonElement> it = metrics.iterator();
    while (it.hasNext()) {
        JsonElement metricElement = it.next();
        locators.add(metricElement.getAsString());
    }/*from  w  ww  .  j  av  a  2 s.c  o m*/

    return locators;
}