Solved Plugin update checker

Discussion in 'Spigot Plugin Development' started by robertlit, Oct 8, 2019.

  1. I used this method that I found in an old thread here
    Code (Java):
    public boolean isOutdated() {
            try {
                   HttpURLConnection c = (HttpURLConnection)new URL("http://www.spigotmc.org/api/general.php").openConnection();
                   c.setDoOutput(true);
                   c.setRequestMethod("POST");
                   c.getOutputStream().write(("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=71792").getBytes("UTF-8"));
                   String newVersion = new BufferedReader(new InputStreamReader(c.getInputStream())).readLine(); //ERROR is here
                   c.disconnect();
                   String oldVersion = this.getDescription().getVersion();
                   if(!newVersion.equals(oldVersion)) {
                     return true;
                   }
                 }
                 catch(Exception e) {
                   System.out.println(ConsoleColors.RED + "Failed to check for updates" + ConsoleColors.RESET);
                   e.printStackTrace();
                 }
            return false;
        }
    It doesn't work, newVersion is null. Does anyone know how to correctly read the version?

    Thanks in advance.
     
  2. If I got an error on a third party API I'm always using the program called Postman to see if the api is working correctly and to learn how to use their API.
     
  3. I use this code in my add-ons, I can't explain how it works because I don't understand it completely.
    Note that this verification will only occur when you start or reload the server, so if you want to check periodically, you can use a repeating task.

    Code (Text):
    PluginDescriptionFile pdffile = getDescription();
    public String latestVersion;
    public String version = pdffile.getVersion();
     
    public String getVersion() { return version; }
    public String getLatestVersion() { return latestVersion; }
     
    public void checkedUpdate() {
            try {
                HttpURLConnection con = (HttpURLConnection)(new URL("https://api.spigotmc.org/legacy/update.php?resource=*****")).openConnection();
                latestVersion = (new BufferedReader(new InputStreamReader(con.getInputStream()))).readLine();
                if(!version.equals(latestVersion)) {
                    //new version messages
                }
            } catch(Exception ex) {
                //error messages
         }
    }
    edit: in the asterisks(*) you just have to put the id of your project.
     
  4. @ComplexCode Thank you very much, I changed the url to the one that you mentioned deleted a few lines and it works now!

    EDIT: for anyone viewing this in the future is used this
    Code (Text):
    public boolean isOutdated() {
            try {
                   HttpURLConnection c = (HttpURLConnection)new URL("https://api.spigotmc.org/legacy/update.php?resource=71792").openConnection();
                   String newVersion = new BufferedReader(new InputStreamReader(c.getInputStream())).readLine();
                   c.disconnect();
                   String oldVersion = this.getDescription().getVersion();
                   System.out.println(newVersion);
                   if(!newVersion.equals(oldVersion)) {
                     return true;
                   }
                 }
                 catch(Exception e) {
                   System.out.println(ConsoleColors.RED + "Failed to check for updates" + ConsoleColors.RESET);
                   e.printStackTrace();
                 }
            return false;
        }