Get name from UUID json cast error

Discussion in 'Spigot Plugin Development' started by Boomber, Aug 31, 2017.

  1. Hello, I have code:

    Code (Text):
        public static String getName(UUID uuid) throws Exception {
            URL url = new URL("https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStreamReader reader = new InputStreamReader(connection.getInputStream());
            JsonArray array = (JsonArray) jsonParser.parse(reader);
            JsonObject object = (JsonObject) array.get(array.size() - 1);
            connection.disconnect();
            reader.close();
            return object.get("name").getAsString();
        }
    Code (Text):
    public void addOnlineTime(ArrayList<UUID> uuidList)
        throws SQLException, ClassNotFoundException
      {
        if (isClosed()) {
          openConnection();
        }
        Statement statement = this.connection.createStatement();
        for (UUID uuids : uuidList) {
          statement.addBatch("INSERT INTO playtime (playeruuid, playtime, username) VALUES ('" + uuids + "','1', '" + Utils.getName(uuids) + "') ON DUPLICATE KEY UPDATE playtime = playtime + 1;");
        }
        statement.executeBatch();
        statement.close();
      }
    I get error:

    Code (Text):
    10:32:54 [SEVERE] java.lang.ClassCastException: com.google.gson.JsonNull cannot be cast to com.google.gson.JsonArray
    10:32:54 [SEVERE]     at lu.r3flexi0n.bungeeonlinetime.utils.Utils.getName(Utils.java:30)
    How I Can fix?
     
  2. Mas

    Mas

    To fetch a JsonArray, use JsonElement#asJsonArray, trying to cast like what you're doing doesn't work. Likewise to get the JsonObject, do JsonElement#asJsonObject.
     
  3. Code (Text):
        public static String getNameAr(UUID uuid) throws Exception {
            URL url = new URL("https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStreamReader reader = new InputStreamReader(connection.getInputStream());
            JsonArray array = (JsonArray) jsonParser.parse(reader);
            JsonElement object = (JsonObject) array.get(array.size() - 1);
            connection.disconnect();
            reader.close();
            return object.get("name").getAsString();
        }
    ?
     
  4. Mas

    Mas

    JsonParser#parse(reader) returns a JsonElement, use JsonElement#getAsJsonArray(), and then once you've fetched the last item in the array, use JsonElement#getAsJsonObject() to get the JsonObject.
     
  5. Code (Text):
        public static String getNameAr(UUID uuid) throws Exception {
            URL url = new URL("https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStreamReader reader = new InputStreamReader(connection.getInputStream());
            JsonElement array = (JsonElement) jsonParser.parse(reader).getAsJsonArray();
            JsonElement object = (JsonElement) ((JsonArray) array).get(((JsonArray) array).size() - 1).getAsJsonObject();
            connection.disconnect();
            reader.close();
            return ((JsonObject) object).get("name").getAsString();
        }
    Please check, this is correctly?
     
  6. Mas

    Mas

    @Boomber

    No, read what I said carefully.
     
  7. Code (Text):
        public static String getNameAr(UUID uuid) throws Exception {
            URL url = new URL("https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStreamReader reader = new InputStreamReader(connection.getInputStream());
            JsonArray array = (JsonArray) jsonParser.parse(reader).getAsJsonArray();
            JsonObject object = (JsonObject) array.get(array.size() - 1).getAsJsonObject();
            connection.disconnect();
            reader.close();
            return object.get("name").getAsString();
        }
    This is not working.
     
  8. Mas

    Mas

    Well tell us the error you get, seriously, we can't do anything if you don't give us any information...
    Also you can get rid of the casts since those methods already return the relevant types.
     
  9. I got this to work, tell me if you have any errors.

    Code (Text):
        public static List<String> getNameHistory(UUID uuid) throws Exception {
            String url_ = "https://api.mojang.com/user/profiles/%s/names";
            URL url = new URL(String.format(url_, uuid.toString().replace("-", "")));
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            String s = IOUtils.toString(connection.getInputStream());
            JsonArray array = new JsonParser().parse(s).getAsJsonArray();
            List<String> names = Lists.newArrayList();
            StreamSupport.stream(array.spliterator(), false).filter(jsonElement -> jsonElement instanceof JsonObject).forEach(jsonElement -> {
                JsonObject jsonObject = (JsonObject) jsonElement;
                names.add(jsonObject.get("name").getAsString());
            });
            return names;
        }

        public static String getLastName(UUID uuid) throws Exception {
            List<String> history = getNameHistory(uuid);
            return history.get(history.size()-1);
        }
    Use getNameHistory to get all player names, and getLastName to get player name.
     
  10. Mas

    Mas

    Another note - if you aren't already, you should perform thos request async as to not block the main thread.
     
  11. You don't necessarily have to do this with Json, you can get it through UUID.fromString();