markfreeman markfreeman - 4 months ago
362 0

No description

Java

Halo 5 Guardian API Example

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;

import org.json.simple.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Main {
	private static APIKey key = new APIKey();
	static String api_key = key.api;
	private static final String match = "https://www.haloapi.com/stats/h5/custom/matches/e55ca5ec-07e3-4c56-b03f-ded301c06e4a";	//Match ID
	public static void main(String[] args) {
	  int team1 = 0;	//Team Liquid Kills
	  int team2 = 0;	//EG Kills
	  try{
	    URL URL = new URL(match);
	    HttpURLConnection urlConnection = (HttpURLConnection)URL.openConnection();
	    urlConnection.setRequestMethod("GET");
	    urlConnection.setRequestProperty("Ocp-Apim-Subscription-Key", api_key);
	    BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
	    JSONParser parser = new JSONParser();	
		JSONObject APIResponse = (JSONObject) parser.parse(in);	//Parse response as JSON object
		JSONArray PlayerStatsArray = (JSONArray) APIResponse.get("PlayerStats");	//Get the player stats array
		JSONObject PlayerStats = new JSONObject();
		Iterator player = PlayerStatsArray.iterator();	//For all players
		while (player.hasNext()){
			JSONObject Gamertag  = (JSONObject) player.next();
			JSONObject PlayerTag = (JSONObject) Gamertag.get("Player");	//Gets the contents of the player field
			String player2 = PlayerTag.get("Gamertag").toString(); //Gets the players gamertag
			int kills = new Integer (Gamertag.get("TotalKills").toString()); //Gets the players total kills
			if (Gamertag.get("TeamId").toString().equals("1")){
				team1 += kills;
			}
			else {
				team2 += kills;
			}
		}
		in.close();
	  } catch (IOException e){
		  e.printStackTrace();
	  } catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  System.out.println("EG Kills: " + team2);
	  System.out.println("Team Liquid Kills: " + team1);
  }
}