A story about hitting the League Of Legends API with JAVA

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)

What I learned this time

--Read API from lol --Load API with JAVA --Use JSON data in JAVA

environment

Load API from lol

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 = " at the end.

Load API with JAVA

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.

Use JSON data in JAVA

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) …….

At the end

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.

References

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

A story about hitting the League Of Legends API with JAVA
A story about the JDK in the Java 11 era
The story of making a game launcher with automatic loading function [Java]
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
The story of making a reverse proxy with ProxyServlet
A story about developing ROS called rosjava with java
The story of making dto, dao-like with java, sqlite
[Jackson] A story about converting the return value of BigDecimal type with a custom serializer.
Story of making a task management application with swing, java
A story packed with the basics of Spring Boot (solved)
About the behavior when doing a file map with java
The story of building a Java version of Minecraft server with GCP (and also set a whitelist)
The story of the first Rails app refactored with a self-made helper
[Note] A story about changing Java build tools with VS Code
A story about having a hard time aligning a testing framework with Java 6
A story that struggled with the introduction of Web Apple Pay
About the treatment of BigDecimal (with reflection)
The story of writing Java in Emacs
The story of forgetting to close a file in Java and failing
The story of low-level string comparison in Java
The story of making ordinary Othello in Java
A story about Java 11 support for Web services
About the description order of Java system properties
About the idea of anonymous classes in Java
The story of learning Java in the first programming
The story of tuning android apps with libGDX
A story about making a Builder that inherits the Builder
A story about trying to operate JAVA File
A story confirming the implementation of the SendGrid Java library when mail delivery fails
Validate the identity token of a user authenticated with AWS Cognito in Java
[PHP] A story about outputting PDF with TCPDF + FPDI
CI the architecture of Java / Kotlin applications with ArchUnit
A memo about the types of Java O/R mappers and how to select them
Use Java lambda expressions outside of the Stream API
Monitor the internal state of Java programs with Kubernetes
Check the behavior of Java Intrinsic Locks with bpftrace
Write a test by implementing the story of Mr. Nabeats in the world with ruby
A story about trying to get along with Mockito
[Java] When writing the source ... A memorandum of understanding ①
A story about reducing memory consumption to 1/100 with find_in_batches
A story about introducing Evolutions into the Play Framework
I learned about the existence of a gemspec file
Dreaming of easily creating a Web API for the DB of an existing Java system
Let's implement a function to limit the number of access to the API with SpringBoot + Redis
A survey of the Kubernetes native Java framework Quarkus
[Java] Cut out a part of the character string with Matcher and regular expression
I tried to make a product price comparison tool of Amazon around the world with Java, Amazon Product Advertising API, Currency API (2017/01/29)
A story about making catkin_make of rosjava compatible offline
Replace only part of the URL host with java
[Java] The problem that true was returned as a result of comparing Integer with ==
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
[First post] A story about a second new graduate of PE becoming a programmer with no experience
A quick explanation of the five types of static in Java
20190803_Java & k8s on Azure The story of going to the festival
Check the operation of two roles with a chat application
A note about the seed function of Ruby on Rails
Implementation of a math parser with recursive descent parsing (Java)
Create a SlackBot with AWS lambda & API Gateway in Java
A story addicted to toString () of Interface proxied with JdkDynamicAopProxy
[Java] A story about IntelliJ IDEA teaching Map's putIfAbsent method
Try hitting the zip code search API with Spring Boot