Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * Project created by ExpDev
- */
- public class PastebinJson {
- /**
- * Runs when the program starts
- */
- public static void main(String[] strings) {
- // The endpoint which we will get the data from
- String requestUrl = "https://pastebin.com/raw/JVPCnziR";
- // Our string builder which we will append our results to later
- StringBuilder sb = new StringBuilder();
- try {
- // Using the String and turning into an URL object
- URL url = new URL(requestUrl);
- // Opening a connection with our URL
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- // The connection should be able to handle outputs
- connection.setDoOutput(true);
- // Using a simple GET
- connection.setRequestMethod("GET");
- // We want JSON back
- connection.setRequestProperty("Accept", "application/json");
- connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
- // reading our response
- BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String line;
- while ((line = br.readLine()) != null) {
- sb.append(line);
- }
- // Closing the reader
- br.close();
- // Disconnecting the connection is also important
- connection.disconnect();
- } catch (Exception e) {
- throw new RuntimeException(e.getMessage());
- }
- String response = sb.toString();
- System.out.println(response);
- JSONObject jsonObject = new JSONObject(response);
- double currentVer = 2.0;
- String name = jsonObject.getString("name");
- double latestVer = jsonObject.getDouble("latestVersion");
- System.out.println("Newest version of " + name + " is: " + (latestVer));
- System.out.println("Have to update?: " + (latestVer > currentVer));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement