Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Summoner{
- private long id;
- private String name;
- private int profileIconId;
- private long revisionDate;
- private long summonerLevel;
- private static final Gson gson = new Gson();
- public static Gson getGson() {
- return gson;
- }
- public long getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public int getProfileIconId() {
- return profileIconId;
- }
- public long getRevisionDate() {
- return revisionDate;
- }
- public long getSummonerLevel() {
- return summonerLevel;
- }
- public static Summoner getByName(String region, String summonerName){
- URL url = generateUrl(region, summonerName);
- System.out.println(url);
- try {
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setDoInput(true);
- connection.setUseCaches(false);
- connection.setRequestProperty("User-Agent", "Pentalytics");
- connection.setRequestProperty("Accept-Language", "en-US");
- connection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8");
- if (connection.getResponseCode() != 200) {
- throw new RuntimeException(String.format("Wrong response code: %s (%s)", connection.getResponseCode(), connection.getResponseMessage()));
- }
- InputStream in = connection.getInputStream();
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- byte[] buffer = new byte[8192];
- int length;
- while ((length = in.read(buffer)) != -1) {
- out.write(buffer, 0, length);
- }
- String charset = connection.getHeaderField("Content-Type").split("charset=")[1].trim();
- String json = out.toString(charset);
- System.out.println(json);
- return gson.fromJson(json, Summoner.class);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- private static URL generateUrl(String region, String summonerName){
- try {
- return new URL(String.format("https://%s.api.pvp.net/api/lol/%s/v1.4/summoner/by-name/%s?api_key=%s", region, region, summonerName, apiKey));
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement