I've studied hard so I'll leave it. Since I have just studied, please point out any mistakes or strange points. (Especially strictly different)
--Read API from lol --Load API with JAVA --Use JSON data in JAVA
There is a developer site published by riot games. I need a lol account (Japanese version is also acceptable), so if you don't have one, get one. https://developer.riotgames.com/ (English site) You will receive the DEVELOPMENT API KEY here. It seems that it can be used for 24 hours only.
If you want to use it with a service like OP.GG (http://jp.op.gg/), you can't use this API KEY, so maybe you can do it by pressing the red button "Register Project" on the upper right. It seems, but this time I used a limited one.
After successfully obtaining (copying) the API KEY, click "GETTING STARTED" in "API DOCUMENTATION" from the tab above to display what seems to be a tutorial. This time, I tried the REGISTERING FOR THE RIOT GAMES API. According to the explanation, it seems that you can receive the API by adding the API KEY obtained earlier to the end of the URL below and accessing it with a browser.
https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/RiotSchmick?api_key=
If Riot Schmick's data is displayed, it is successful. This time, I got Riot Schmick's player information. If you want to get your own information, change the "RiotSchmick" part of the url to your summoner name, and if the server does not appear in NA, change the "na1" part to your server area ("jp1" in Japan). I will.
If you want to load other APIs, click "API DCUMENTATION" on the developer site and select the data you want to get.
For example, if you want to load champion data, select "CHAMPION-V3" from the left tab, click "/ lol / platform / v3 / champions", select an option (optional) and click "EXECUTE REQUEST". The URL and acquisition result will be displayed. If you want to check it yourself, you can copy the URL and open it from your browser. At this time, if you open it normally, it will be rejected with an authentication error, so add "& api_key =
If there is no problem, read it with JAVA. However, I don't know how to handle JAVA strings, so I'll just paste the code.
LolApiTest.java
import java.io.*;
import java.net.URL;
public class LolApiTest {
public static void main(String[] args){
String region = "na1";
String apiKey = //Put the obtained API KEY here
String apiUrl = "https://"+region+".api.riotgames.com/lol/summoner/v3/summoners/by-name/RiotSchmick?api_key="+apiKey;
try {
//I don't know here
URL url = new URL(apiUrl);
String newLine = ""; //Get a new line here
String currentText = ""; //Add all the acquired lines here
//I don't know these two
InputStreamReader isReader = new InputStreamReader(url.openStream());
BufferedReader bReader = new BufferedReader(isReader);
//Add until the fetched line becomes null
while((newLine = bReader.readLine()) != null){
currentText = currentText + newLine;
}
//Output at the end
System.out.println(currentText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
If the same output as when loaded by the browser is output, it is successful.
It seems that the format with many output {} and: is called JSON data. (Please tell me the details) It's a pain to read this on your own and drop it into a list or array, so I'll use the help of the library. This time I borrowed JSON in Java from the URL below. I think it's okay because the version is the newest.
https://mvnrepository.com/artifact/org.json/json
To add the downloaded library, in Eclipse, right-click the project, select "Add External Archive" in "Build Path", and select the downloaded library. Specifically, I added and used it as shown below.
LolApiTest.java
/*First omitted*/
import org.json.JSONObject;
/*Omission*/
//Create a JSON object.
JSONObject summonerObject = new JSONObject(currentText);
//Output all
for(String key: summonerObject.keySet()){
System.out.println(key+": "+summonerObject.get(key));
}
/*The back is also omitted*/
Of the JSON data read, the one on the left side of: such as "accountId" and "profileIconId": seems to be called Key. The Key of the created object can be obtained by the keySet method. An array of Strings will be returned. Of the JSON data read, the one to the right of: can be obtained with the get method. I'm not sure what type it will return, but if you want to specify it, you can use getString etc. Enter Key as an argument. In the case of nesting where the list is contained in the element of the list, it seems that you can get it by doing object.get (key1) .get (key2) …….
When I thought, "Let's do this!", There was no article that pinpointed the content (all of them were faint), so I summarized it. (A trendy summary article?) I hope it helps subsequent people to create amazing tools and web apps (vocabulary).
I still have the ability to go beyond my hobbies (maybe?), So the sentences are hard to read and the content is probably not strict. As I wrote at the beginning, please point out any strange points. (Really)
Originally, when I was soliciting programming issues on twitter, my juniors in the circle told me to read the API for the cost performance table of lol items. It's completed, so I may publish the code if I feel like it. (Especially because it matters whether it works fine even if the lol patch is updated)
Thank you for reading until the end.
Please note that some information is out of date.
――I can't hear you anymore! What is an API? ~ Learn the basics of the basics ~ (https://www.sejuku.net/blog/7087) -[Java] How to handle JSON data with standard API, Jackson, JSON in Java (https://www.sejuku.net/blog/39599) --Android JSON Perth Nesting (http://weblog.4141.biz/?p=406) --I want to hit the API in Java (http://bigbuddha.hatenablog.jp/entry/JAVA_GET_API) --About Riot Games API --hogepiyo (http://reginn666.hatenablog.com/entry/2014/05/16/162400)
The access date for both is March 27, 2018.
Recommended Posts