Last active
September 11, 2019 17:24
-
-
Save Rizwan-Hasan/246a8b6ee067cef14e24da1ffe19577c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Filename: GitApi.java | |
Used module from https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1 | |
*/ | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
import org.json.simple.parser.ParseException; | |
import javax.net.ssl.HttpsURLConnection; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Scanner; | |
public class GitApi { | |
public static void main(String[] args) { | |
String userName = "magpie-robins"; // User name of the repository | |
String repoName = "gpa-calculator-android"; // Repository name | |
String releaseTag = "latest"; // Using tag 'latest' for latest released download count | |
String apiUrl = "https://api.github.com/repos/" + userName + "/" + repoName + "/releases" + "/" + releaseTag; | |
try { | |
URL url = new URL(apiUrl); | |
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); | |
connection.setRequestMethod("GET"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
String jsonString = new Scanner(connection.getInputStream()).useDelimiter("\\A").next(); | |
JSONObject jsonObject = (JSONObject) new JSONParser().parse(jsonString); | |
jsonObject = (JSONObject) ((JSONArray) jsonObject.get("assets")).get(0); | |
Long downloadCount = (Long) jsonObject.get("download_count"); | |
System.out.println("Total downloads: " + downloadCount); | |
} catch (ParseException | IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment