-1

I'm trying to make an update checker for a game, where it reads the latest update version from a file online and notifies the user if the version is higher than the current version of the game.

The version file is fully public on Dropbox and contains only the number 2.

This is what I'm currently trying:

public static int getVersionFromCloud()
{
    int version = -1;

    try
    {
        URL link = new URL("https://www.dropbox.com/s/1jeuy4v1w46ccn6/launcher.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(link.openStream()));

        System.out.println(reader.readLine());
        version = Integer.parseInt(reader.readLine());

        reader.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return version;
}

But I get this error:

// From System.out.println()
<!DOCTYPE html><html lang="en" xmlns:fb="http://ogp.me/ns/fb#" xml:lang="en" class="" xmlns="http://www.w3.org/1999/xhtml">
// Error
Exception in thread "main" java.lang.NumberFormatException: For input string: "<head><script type="text/javascript" nonce="CYFUWuYzTUo6rNiFN6N4">"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at data.io.Util.getVersionFromCloud(Util.java:21)
at data.LauncherBasic.updateFields(LauncherBasic.java:104)
at data.LauncherBasic.<init>(LauncherBasic.java:55)
at data.LauncherBasic.main(LauncherBasic.java:43)

I've also tried this, but it always returns null.

Community
  • 1
  • 1
  • your parameter for the Integer.parseInt(reader.readLine()); is not an integer so you get a NumberFormatException – RAZ_Muh_Taz Oct 14 '16 at 20:51

1 Answers1

0

Problem

When you open your dropbox url you don't get the file content but dropbox page html code, so you fail on trying convert line with its html tags (<head><script type="text/javascript" nonce="CYFUWuYzTUo6rNiFN6N4">) to number.

Solution

Change the url to https://dl.dropboxusercontent.com/s/1jeuy4v1w46ccn6/launcher.txt (like the way it described in the link you've provided) and it will work fine and you'll get your 2:

URL link = new URL("https://dl.dropboxusercontent.com/s/1jeuy4v1w46ccn6/launcher.txt");
Cœur
  • 37,241
  • 25
  • 195
  • 267
Filipp Voronov
  • 4,077
  • 5
  • 25
  • 32
  • Okay great, that worked. But it still won't read it as an integer. I'm now getting: – Miss_Apocalypse Oct 14 '16 at 21:40
  • 2 Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at data.io.Util.getVersionFromCloud(Util.java:21) at data.LauncherBasic.checkForUpdates(LauncherBasic.java:123) at data.LauncherBasic.updateFields(LauncherBasic.java:114) at data.LauncherBasic.(LauncherBasic.java:63) at data.LauncherBasic.main(LauncherBasic.java:49) – Miss_Apocalypse Oct 14 '16 at 21:41
  • Never mind, I was being dumb. System.out.print() already used the line "2" so it was trying to parse the next line which didn't exist. Thank you. – Miss_Apocalypse Oct 14 '16 at 21:49