Advertisement
Guest User

JsonGet

a guest
Feb 17th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. package xyz.olympiccode.tools.ocbot.others;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7.  
  8. import com.google.gson.JsonObject;
  9. import com.google.gson.JsonParser;
  10.  
  11. public class JsonGet {
  12.  
  13.  
  14.  
  15.     private static String BASE_URL = "https://eu.mc-api.net/v3/uuid/";
  16.  
  17.     public static String getJson(final String player) {
  18.         String url = BASE_URL + player;
  19.         String content = "";
  20.         try {
  21.             HttpURLConnection con = createConnection(url);
  22.  
  23.             BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
  24.             String input;
  25.             while ((input = br.readLine()) != null) {
  26.                 content = content + input;
  27.             }
  28.             br.close();
  29.         } catch (Exception e) {
  30.             e.printStackTrace();
  31.         }
  32.         return content;
  33.     }
  34.    
  35.     public static String getUUID(String player) {
  36.     JsonObject jobject = new JsonObject();
  37.      JsonParser parser = new JsonParser();
  38.      String json = JsonGet.getJson(player);
  39.      
  40.      jobject = (JsonObject)parser.parse(json);
  41.      
  42.     String puuid = null;
  43.      try
  44.      {
  45.          puuid = jobject.get("uuid").getAsString();
  46.      }
  47.      catch (Exception e) {}
  48.      
  49.      return puuid;
  50.     }
  51.  
  52.    
  53.     private static HttpURLConnection createConnection(String s) throws Exception {
  54.         URL url = new URL(s);
  55.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  56.         connection.setUseCaches(true);
  57.         connection.addRequestProperty("User-Agent", "Mozilla/4.76");
  58.         connection.setDoOutput(true);
  59.         return connection;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement