Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import org.json.JSONObject;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6.  
  7. /**
  8.  * Project created by ExpDev
  9.  */
  10.  
  11.  
  12. public class PastebinJson {
  13.  
  14.     /**
  15.      * Runs when the program starts
  16.      */
  17.     public static void main(String[] strings) {
  18.         // The endpoint which we will get the data from
  19.         String requestUrl = "https://pastebin.com/raw/JVPCnziR";
  20.  
  21.         // Our string builder which we will append our results to later
  22.         StringBuilder sb = new StringBuilder();
  23.         try {
  24.             // Using the String and turning into an URL object
  25.             URL url = new URL(requestUrl);
  26.             // Opening a connection with our URL
  27.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  28.  
  29.             // The connection should be able to handle outputs
  30.             connection.setDoOutput(true);
  31.             // Using a simple GET
  32.             connection.setRequestMethod("GET");
  33.             // We want JSON back
  34.             connection.setRequestProperty("Accept", "application/json");
  35.             connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
  36.  
  37.             // reading our response
  38.             BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  39.             String line;
  40.             while ((line = br.readLine()) != null) {
  41.                 sb.append(line);
  42.             }
  43.             // Closing the reader
  44.             br.close();
  45.             // Disconnecting the connection is also important
  46.             connection.disconnect();
  47.         } catch (Exception e) {
  48.             throw new RuntimeException(e.getMessage());
  49.         }
  50.         String response = sb.toString();
  51.         System.out.println(response);
  52.  
  53.         JSONObject jsonObject = new JSONObject(response);
  54.  
  55.         double currentVer = 2.0;
  56.         String name = jsonObject.getString("name");
  57.         double latestVer = jsonObject.getDouble("latestVersion");
  58.  
  59.         System.out.println("Newest version of " + name + " is: " + (latestVer));
  60.         System.out.println("Have to update?: " + (latestVer > currentVer));
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement