Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. public class Summoner{
  2.  
  3.     private long id;
  4.     private String name;
  5.     private int profileIconId;
  6.     private long revisionDate;
  7.     private long summonerLevel;
  8.  
  9.     private static final Gson gson = new Gson();
  10.     public static Gson getGson() {
  11.         return gson;
  12.     }
  13.  
  14.     public long getId() {
  15.         return id;
  16.     }
  17.  
  18.     public String getName() {
  19.         return name;
  20.     }
  21.  
  22.     public int getProfileIconId() {
  23.         return profileIconId;
  24.     }
  25.  
  26.     public long getRevisionDate() {
  27.         return revisionDate;
  28.     }
  29.  
  30.     public long getSummonerLevel() {
  31.         return summonerLevel;
  32.     }
  33.  
  34.     public static Summoner getByName(String region, String summonerName){
  35.         URL url = generateUrl(region, summonerName);
  36.         System.out.println(url);
  37.         try {
  38.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  39.             connection.setDoInput(true);
  40.             connection.setUseCaches(false);
  41.             connection.setRequestProperty("User-Agent", "Pentalytics");
  42.             connection.setRequestProperty("Accept-Language", "en-US");
  43.             connection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8");
  44.  
  45.             if (connection.getResponseCode() != 200) {
  46.                 throw new RuntimeException(String.format("Wrong response code: %s (%s)", connection.getResponseCode(), connection.getResponseMessage()));
  47.             }
  48.  
  49.             InputStream in = connection.getInputStream();
  50.             ByteArrayOutputStream out = new ByteArrayOutputStream();
  51.  
  52.             byte[] buffer = new byte[8192];
  53.             int length;
  54.             while ((length = in.read(buffer)) != -1) {
  55.                 out.write(buffer, 0, length);
  56.             }
  57.  
  58.             String charset = connection.getHeaderField("Content-Type").split("charset=")[1].trim();
  59.  
  60.             String json = out.toString(charset);
  61.             System.out.println(json);
  62.             return gson.fromJson(json, Summoner.class);
  63.         } catch (IOException e) {
  64.             e.printStackTrace();
  65.         }
  66.  
  67.         return null;
  68.  
  69.  
  70.     }
  71.  
  72.     private static URL generateUrl(String region, String summonerName){
  73.         try {
  74.             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));
  75.         } catch (MalformedURLException e) {
  76.             e.printStackTrace();
  77.         }
  78.         return null;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement