Java tutorial
package com.tbs.devcorner.simple; /** * Simple example of a Java program used to query an API. * @license http://data.gc.ca/eng/open-government-licence-canada * * Maven Dependency: * <dependency> * <groupId>org.json</groupId> * <artifactId>json</artifactId> * <version>20131018</version> * </dependency> */ import java.io.*; import java.net.*; import org.json.*; public class App { public static void main(String[] args) { try { // Build Connection URL api_url = new URL("http://www.earthquakescanada.nrcan.gc.ca/api/earthquakes/"); URLConnection api = api_url.openConnection(); // Set HTTP Headers api.setRequestProperty("Accept", "application/json"); api.setRequestProperty("Accept-Language", "en"); // Get Response JSONTokener tokener = new JSONTokener(api.getInputStream()); JSONObject jsondata = new JSONObject(tokener); // Display API name System.out.println(jsondata.getJSONObject("metadata").getJSONObject("request").getJSONObject("name") .get("en").toString()); // Iterate over latest links JSONObject latest = jsondata.getJSONObject("latest"); for (Object item : latest.keySet()) { System.out.println(item.toString() + " -> " + latest.get(item.toString())); } } catch (MalformedURLException e) { System.out.println("Malformed URL"); } catch (IOException e) { System.out.println("IO Error"); } } }