I am trying to make a UWP app using the RIOT API for League of Legends.
When I go to their website to generate the JSON I get something like this:
{"gigaxel": {
"id": 36588106,
"name": "Gigaxel",
"profileIconId": 713,
"revisionDate": 1451577643000,
"summonerLevel": 30
}}
When I select this JSON and copy it into a new class using the special paste method in Visual Studio 2015 I get these classes with these properties:
public class Rootobject
{
public Gigaxel gigaxel { get; set; }
}
public class Gigaxel
{
public int id { get; set; }
public string name { get; set; }
public int profileIconId { get; set; }
public long revisionDate { get; set; }
public int summonerLevel { get; set; }
}
I created a new class called LOLFacade
for connecting to the RiotAPI:
public class LOLFacade
{
private const string APIKey = "secret :D";
public async static Task<Rootobject> ConnectToRiot(string user,string regionName)
{
var http = new HttpClient();
string riotURL = String.Format("https://{0}.api.pvp.net/api/lol/{0}/v1.4/summoner/by-name/{1}?api_key={2}",regionName, user, APIKey);
var response = await http.GetAsync(riotURL);
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Rootobject>(result);
}
}
This is the button event handler method:
Rootobject root = new Rootobject { gigaxel = new Gigaxel() };
root = await LOLFacade.ConnectToRiot("gigaxel","EUNE");
string name = root.gigaxel.name;
int level = root.gigaxel.summonerLevel;
InfoTextBlock.Text = name + " is level " + level;
I hard coded the regionName and the user for testing purposes. This works with my username: "gigaxel".
When I try another username for example like "xenon94" I get an exception:
Object reference not set to an instance of an object.
When I change the property name in Rootobject from gigaxel to xenon94 like this:
public class Rootobject
{
public Gigaxel xenon94 { get; set; }
}
When I recompile my code it works for the username xenon94
but it doesn't work for my username "gigaxel"
.
I want it to work for any given username.