class Http {
final static String API_KEY = `라이엇 개인용 api_key`;
public Http(String nickname) {
getSummonerInfo(nickname);
}
static String getSummonerInfo(String nickname) {
HttpURLConnection conn = null;
String jsonString = null;
try {
String SummonerName = nickname.replaceAll(" ", "%20");
URL url = new URL("https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/"+ SummonerName+"?api_key="+API_KEY);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Transfer-Encoding", "chunked");
conn.setRequestProperty("Connection", "keep-alive");
conn.setDoOutput(true);
// 서버로부터 데이터 읽어오기
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) { // 읽을 수 있을 때 까지 반복
sb.append(line);
}
JSONObject obj = new JSONObject(sb.toString()); // json으로 변경 (역직렬화)
jsonString = "{"
+ "\"id\": \"" + obj.getString("id") + "\","
+ "\"accountId\": \"" + obj.getString("accountId") + "\","
+ "\"puuid\": \"" + obj.getString("puuid") + "\","
+ "\"name\": \"" + obj.getString("name") + "\","
+ "\"profileIconId\": \"" + obj.getInt("profileIconId") + "\","
+ "\"revisionDate\": \"" + obj.getLong("revisionDate") + "\","
+ "\"summonerLevel\": \"" + obj.getInt("summonerLevel") + "\","
+ "}";
} catch (Exception e) {
e.printStackTrace();
}
return jsonString;
}
URL url = new URL("https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/"+ SummonerName+"?api_key="+API_KEY);