Http connection

Discussion in 'Plugin Development' started by JMteam09, Feb 23, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    TheEntropy

    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();
    
    }
    
     
  2. Offline

    Gingerbreadman

    @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);
     
    Last edited by a moderator: Feb 23, 2015
  3. 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!
     
    Last edited: Feb 23, 2015
  4. Offline

    timtower Administrator Administrator Moderator

    @JMteam09 Use the edit button instead of double posting please. Merged 5 posts in 50 minutes.
     
  5. Offline

    Gingerbreadman

    @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!)
     
  6. Offline

    Skionz

    @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
    }
     
  7. Offline

    TheEntropy

    @JMteam09 It worked fine :p

    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}
     
    Last edited: Feb 23, 2015
  8. Offline

    Gingerbreadman

    nvm, didn't see your last edit xD
     
  9. Offline

    stormneo7

    [​IMG]
    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]);
        }
     
  10. Offline

    Mister_Fix

    Hello! i'm a begginer java developer. can you please explain why do i need this buffer stuff everytime i deal with HTTP connections?
     
  11. Offline

    Skionz

    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
     
Thread Status:
Not open for further replies.

Share This Page