I want to make a plugin that checks the current version from an online .txt file. I dont know how to use HTTP requests. Anyone knows how to do this? File url: https://github.com/JMteam09/PluginFiles/blob/master/Quests/version.txt
Try something like: Code: try { URL url = new URL("https://github.com/JMteam09/PluginFiles/blob/master/Quests/version.txt"); Scanner s = new Scanner(url.openStream()); // This should work for basic applications :P } catch (IOException e) { // This should only run if the file doesn't exist on the server, you typed the URL incorrectly, or // you couldn't connect to the server (or don't have permissions to view it). e.printStackTrace(); }
@JMteam09 Mate, I did this before, I will send it to you in a min Here do this Code: BufferedReader br = new BufferedReader(new InputStreamReader(new URL(versionUrl).openStream())); String response =br.readLine);
ill try this out @Gingerbreadman Your method didnt return the 2.5 version number in this code: Code: BufferedReader br; String response = "X"; try { br = new BufferedReader(new InputStreamReader(new URL("https://github.com/JMteam09/PluginFiles/blob/master/TabAPIversion.txt").openStream())); response = br.readLine(); } catch (IOException e) { e.printStackTrace(); } Bukkit.getServer().getLogger().info(""); Bukkit.getServer().getLogger().info(""); Bukkit.getServer().getLogger().info("[TabAPI]: Checking for new versions!"); if(this.getDescription().getVersion().equals(response)){ if(Integer.valueOf(response) != null || Integer.valueOf(response) != 0){ Bukkit.getServer().getLogger().info("[TabAPI]: There is an newer version avaible! (Version: " + response + ""); } } else { if(response.equals("X")){ Bukkit.getServer().getLogger().info("[TabAPI]: Error while loading versions from online data!"); } else { Bukkit.getServer().getLogger().info("[TabAPI]: There is no newer version avaible! (Current version: " + response + ")"); } } Bukkit.getServer().getLogger().info(""); Bukkit.getServer().getLogger().info(""); } TabAPI is not part of the plugin TabAPI hosted on bukkit site but its an custom project to display ranks in front of player names in Tab score board. @TheEntropy now trying your method EDIT by Timtower: merged posts Both methods dont seem to work and it doesnt fail on getting the http connection. so maby i am using the wrong url needed to use this url instead: https://raw.githubusercontent.com/JMteam09/PluginFiles/master/TabAPIversion.txt else it did show the github page with its httpcode and then the plugin was unable to specify the text. @TheEntropy Please post how to get the output of it in a string type! @Gingerbreadman got it working with your method. thanks!
@JMteam09 I have to go right now, I can help you out in the morning but you can have this class (AutoUpdater Using Versions) Code: public class UpdateThread extends Thread { private JavaPlugin plugin; private String fileName; private String versionUrl; private double version = Double.parseDouble(plugin.getDescription().getVersion()); private String url; public UpdateThread(JavaPlugin plugin, String fileName, String url, String versionUrl) { this.plugin = plugin; this.fileName = fileName; this.url = url; this.versionUrl = versionUrl; } @Override public void run() throws NumberFormatException { while(true) { try{ BufferedReader br = new BufferedReader(new InputStreamReader(new URL(versionUrl).openStream())); double response = Double.parseDouble(br.readLine()); if(response > version) { download(); break; } }catch(Exception e) { } } } private void download() throws IOException { URLConnection http = new URL(url).openConnection(); InputStream in = http.getInputStream(); try{ OutputStream out = new FileOutputStream(fileName); try{ byte[] b = new byte[1024]; int read; while((read = in.read(b)) > 0) out.write(b, 0, read); }finally{ out.close(); } }finally{ in.close(); Bukkit.getServer().reload(); } } } Check out how this one works, and use doubles not integers! (Integers dont have decimals in them!)
@JMteam09 Code: private String getCurrentVersion() { URL url = new URL("https://github.com/JMteam09/PluginFiles/blob/master/Quests/version.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String version = br.readLine(); br.close(); return version; } I can't use GitHub so I am not sure on the specifics, but this assumes there is only one line. If you want to iterate through the file use a while look like this Code: String line; while((line = br.readLine) != null) { //do stuff with line }
@JMteam09 It worked fine Code: try { URL url = new URL("https://raw.githubusercontent.com/JMteam09/PluginFiles/master/Quests/version.txt"); Scanner s = new Scanner(url.openStream()); while (s.hasNext()) { String line = s.next(); if (line.contains("QuestVersion")) { System.out.println(line); break; } } s.close(); } catch (IOException e) { e.printStackTrace(); } EDIT: You need to use raw.githubusercontent.com (don't just use github). That piece of code will print: {QuestVersion:1.5}
Code: public static void main(String args[]) throws Exception { System.out.println("Version: " + getVersion()); } public static double getVersion() throws Exception{ URL url = new URL("https://raw.githubusercontent.com/JMteam09/PluginFiles/master/Quests/version.txt"); Scanner s = new Scanner(url.openStream()); return Double.parseDouble(s.nextLine().split(":")[1].split("}")[0]); }
Hello! i'm a begginer java developer. can you please explain why do i need this buffer stuff everytime i deal with HTTP connections?
That class reads the version, and downloads the new version if it is available. If you simply want to read the version my solution will work. A BufferedReader If you want a more elaborate explanation you can google it, or read the Javadocs: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html http://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html http://docs.oracle.com/javase/7/docs/api/java/net/URL.html http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html