意外的令牌' END OF FILE'位置0(JSON)

时间:2014-07-08 15:57:14

标签: java json parsing token eof

我尝试使用提供的API Mojang通过网站从UUID中检索名称。

这是我使用的代码(我验证它有效):

@Override
public Map<UUID, String> call() throws Exception {
    Map<UUID, String> uuidStringMap = new HashMap<UUID, String>();
    for (UUID uuid : uuids) {
        HttpURLConnection connection = (HttpURLConnection) new URL(
                PROFILE_URL + uuid.toString().replace("-", ""))
                .openConnection();

        JSONObject response;

        System.out.println("String: " + fromStream(connection.getInputStream()));
        System.out.print("Line: " + uuid.toString());

        String name = null;

        try {
            response = (JSONObject) jsonParser
                    .parse(new InputStreamReader(connection.getInputStream()));

            name = (String) response.get("name");
            if (name == null) {
                continue;
            }
            String cause = (String) response.get("cause");
            String errorMessage = (String) response.get("errorMessage");
            if (cause != null && cause.length() > 0) {
                throw new IllegalStateException(errorMessage);
            }
        } catch (Exception e) {
            System.out.print("Could not parse uuid '" + uuid.toString() + "' to name!");
            System.out.print("Trying Bukkit.getOfflinePlayer()");

            OfflinePlayer oPlayer = Bukkit.getOfflinePlayer(uuid);

            if (oPlayer != null && oPlayer.getFirstPlayed() != 0) {
                name = oPlayer.getName();

                System.out.print("Got player " + name);
            } else {
                System.out.print("Could not retrieve player with Bukkit.getOfflinePlayer()");
                e.printStackTrace();
            }
            //throw new Exception("Could not parse uuid '" + uuid.toString() + "' to name!");
        }


        uuidStringMap.put(uuid, name);
    }
    return uuidStringMap;
}

不要介意所有System.out.print行,这只是调试内容。

错误导致此错误:

[17:35:56 WARN]: Unexpected token END OF FILE at position 0.
[17:35:56 WARN]:        at org.json.simple.parser.JSONParser.parse(Unknown Source)
[17:35:56 WARN]:        at org.json.simple.parser.JSONParser.parse(Unknown Source)
[17:35:56 WARN]:        at me.armar.plugins.autorank.util.uuid.NameFetcher.call(
NameFetcher.java:59)
[17:35:56 WARN]:        at me.armar.plugins.autorank.util.uuid.UUIDManager$2.run
(UUIDManager.java:222)
[17:35:56 WARN]:        at java.lang.Thread.run(Unknown Source)

现在,我通过一些JSON在线解析器运行返回的json。他们都告诉我JSON是正确的。这是要返回的JSON:

{"id":"cb994e15a57a4157953ab29577229ebe","name":"tator0 1","properties":[{"name":"textures","value":"eyJ0aW1lc3RhbXAiOjE0MDQ4MzM3NTM0MTYsInByb2ZpbGVJZCI6ImNiOTk0ZTE1YTU3YTQxNTc5NTNhYjI5NTc3MjI5ZWJlIiwicHJvZmlsZU5hbWUiOiJ0YXRvcjAxIiwiaXNQdWJsaWMiOnRydWUsInRleHR1cmVzIjp7IlNLSU4iOnsidXJsIjoiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS80Nzk5MjQ4NGJmNjUyODJhMTQzOWI0MGQ1NWU2ZTRjZGQxNGRmYzAyNGE0MzA3Y2M2YTM1M2Q4MzY1OThkZDUifX19","signature":"t93OEZ3BNa4vo47diKixJA0KWXqblUtWC9yZtTImuxfbSs0khqV02p58eeDF4Qbd4gy6VBQ0zv6N9oxRqhTKBQ1BG1FJHTfhGXFlLYlhHlXmVC5FMBnK0P//0T0PqVz7hlFgzW3YK6vaEf3tz5v5H/S+aCjZbCZ9vehDRx8vrV+SzZek0vuSHjm9ojooqwD4WQjCg1ZPZPtpYD/efj1OcTmS6xEnBEXbCTqOnK/wRbwfyKtezkJutFJ4UCrVCLos85ze79mMfBr67CtotFcQrVqRE9X5zShLatCCVlefPRo6K7TcKKzQYlTM24asaySfZYpv0ujGeHX2c6s4nKvg6vnwLnHT2RwDuJucCojAeoy9pRFq8jEtPSoiPGlA37NlLjChuLXp3cSYGhKIpgDFcXXNF+YsYOlN2XuYa2OpAt8EgDj09gd8r903a2apIVUDo4Hmln9rlI9J74KdifEZc3uUyazlP2BByID2YBcKr5mq5ye+MOKWkN8j5AaPNUKPNpFBXHkOOF/uE72VoBpsjDm0X9OfEx7xtNu+bA4jBObxnQKsD2qIvyhCtlmuN4S6G+VKtsBhiqlO7JTycAn1yij3aw3XhT763gALQNcUkjzERnyp55s8J7FlIpSvObNvndOgypPu4RpsHtkL/G16a/mxRJhljiAasiKX1WsYVig="}],"legacy":true}

我看到还有其他人有类似的问题,here

该意外令牌可以在哪里?

2 个答案:

答案 0 :(得分:2)

在解析之前尝试将.toString()应用于输入字符串。

例如:

DataInputStream in = new DataInputStream(clientSocket.getInputStream());
String dataIn = in.readUTF();
JSONObject jobj = (JSONObject) parser.parse(dataIn.toString());

刚才为我工作过。 :d

答案 1 :(得分:0)

在我的情况下,我试图解析空文件。所以,我在解析之前添加了简单的检查,问题就消失了。