Java tutorial
package com.vdispatcher; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Random; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class GetBaseInformation extends Thread { private final ArrayList<Base> bases = new ArrayList<Base>(); private static final String BASE_URL = "http://nycopendata.socrata.com/api/views/nadh-kjkc/rows.json?accessType=DOWNLOAD"; @Override public void run() { StringBuilder result = new StringBuilder(); URL url = null; HttpURLConnection conn = null; try { url = new URL(BASE_URL); conn = (HttpURLConnection) url.openConnection(); InputStreamReader in = new InputStreamReader(conn.getInputStream()); int read; char[] buff = new char[1024]; while ((read = in.read(buff)) != -1) { result.append(buff, 0, read); } JSONObject jsonObject = new JSONObject(result.toString()); JSONArray data = jsonObject.getJSONArray("data"); for (int i = 1; i < data.length(); i++) { bases.add(Base.fromJSONArray(data.getJSONArray(i))); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } // @SuppressWarnings("unused") public final static class Base { private String licenseNumber; private String licenseeName; private String alternativeLicenseeName; private String streetAddress; private String zipCode; private String telephone; private String city; private double lat, lng; private static final String licenseType = "TLC-licensed base (FHV, Commuter Van, or Para)"; private static final String state = "NY"; private static final String MAPS_API_BASE = "http://maps.googleapis.com/maps/api/geocode"; private static final String OUTPUT_JSON = "/json"; private static final StringBuilder urlBuilder = new StringBuilder(); private static final String OVER_QUERY_LIMIT = "OVER_QUERY_LIMIT"; private Base() { } public String getLicenseNumber() { return licenseNumber; } public String getLicenseeName() { return licenseeName; } public String getAlternativeLicenseeName() { return alternativeLicenseeName; } public String getStreetAddress() { return streetAddress; } public String getZipCode() { return zipCode; } public String getTelephone() { return telephone; } public static final String getLicenseType() { return licenseType; } public String getCity() { return city; } public static final String getState() { return state; } public double getLng() { return lng; } public double getLat() { return lat; } public static Base fromJSONArray(JSONArray jsonBase) { Base base = new Base(); try { JSONObject invalidCells = new JSONObject(jsonBase.getString(INVALID_CELLS)) .getJSONObject("invalidCells"); base.city = format(invalidCells.getString("1512377").trim()); base.licenseNumber = format(jsonBase.getString(LICENSE_NUMBER).trim()); base.licenseeName = format(jsonBase.getString(LICENSEE_NAME).trim()); base.alternativeLicenseeName = format(jsonBase.getString(ALTERNATIVE_LICENSEE_NAME).trim()); base.streetAddress = format(jsonBase.getString(STREET_ADDRESS).trim()); base.zipCode = format(jsonBase.getString(ZIP_CODE).trim()); base.telephone = format(jsonBase.getString(TELEPHONE).trim()); } catch (JSONException e) { e.printStackTrace(); } Base.getLatLngInformation(base); return base; } private static String format(String string) { StringBuilder sb = new StringBuilder(); String[] split = string.split("\\s"); for (int i = 0; i < split.length - 1; i++) { sb.append(split[i] + " "); //make sure there is only one space between words } //append the last element and return it return sb.append(split[split.length - 1]).toString(); } private static void getLatLngInformation(Base base) { String[] split = base.streetAddress.split("[\\s]"); urlBuilder.setLength(0); //clear urlBuilder.append(Base.MAPS_API_BASE + OUTPUT_JSON); urlBuilder.append("?address="); for (int i = 0; i < split.length - 1; i++) { urlBuilder.append(split[i] + "+"); } urlBuilder.append(split[split.length - 1]); urlBuilder.append("&components=country:US|postal_code:" + base.zipCode + "|locality="); split = base.city.split("\\s"); for (int i = 0; i < split.length - 1; i++) { urlBuilder.append(split[i] + "+"); } urlBuilder.append(split[split.length - 1]); urlBuilder.append("&sensor=false"); // System.out.println(urlBuilder.toString()); URL url = null; HttpURLConnection conn = null; InputStreamReader in = null; StringBuilder result = new StringBuilder(); int read; char[] buff = new char[1024]; try { boolean successful = true; do { url = new URL(urlBuilder.toString()); conn = (HttpURLConnection) url.openConnection(); in = new InputStreamReader(conn.getInputStream()); while ((read = in.read(buff)) != -1) { result.append(buff, 0, read); } JSONObject object = new JSONObject(result.toString()); String status = object.getString("status"); if (status.equals(OVER_QUERY_LIMIT)) { successful = false; try { Random r = new Random(); long l = r.nextLong() % 10000; if (l < 0) { l += 10000; } System.out.printf("Sleeping for %s ms\n", l); Thread.sleep(l); } catch (InterruptedException e) { e.printStackTrace(); } continue; } else { successful = true; } System.out.println(object.toString(3)); } while (!successful); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } public String getFormattedAddress() { StringBuilder sb = new StringBuilder(); sb.append(this.streetAddress + ", "); sb.append(this.city + ", "); sb.append(Base.state + ", "); sb.append(this.zipCode); return sb.toString(); } private static final int INVALID_CELLS = 7, LICENSE_NUMBER = 8, LICENSEE_NAME = 9, ALTERNATIVE_LICENSEE_NAME = 10, ZIP_CODE = 12, STREET_ADDRESS = 11, TELEPHONE = 13; } }