0

How do I do this in Java?

import requests

params = {
  "api_key": "asdfg_54321",
  "format": "json"
}
r = requests.get('https://www.parsehub.com/api/v2/projects/qwertyuiop/last_ready_run/data', params=params)
print(r.text)

I need to access an online API in the form of a JSON file, and I can do so in Python, but I need the data for an android studio app, so I have to figure out how to access a URL with API key as a permission in Java instead.

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
  • 2
    Possible duplicate of [Using java.net.URLConnection to fire and handle HTTP requests](http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests) – Lou Franco Apr 13 '17 at 14:00
  • we just had that discussion a second ago: http://stackoverflow.com/questions/43384255/is-there-any-option-to-automate-apis-using-java-and-selenium-webdriver – slowy Apr 13 '17 at 14:11

1 Answers1

0

Make sure to resolve ${PROJECT_TOKEN}, ${API_KEY} and ${FORMAT} variables

public static void getResultOfLastReadyRun() throws IOException {
    URL url = new URL("https://www.parsehub.com/api/v2/projects/${PROJECT_TOKEN}/last_ready_run/data?api_key=${API_KEY}&format=${FORMAT}");

    try(Scanner sc = new Scanner(new GZIPInputStream(url.openStream()))) {
        while (sc.hasNext()) {
            System.out.println(sc.nextLine());
        }
    }
}