Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package animalclient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import javax.ws.rs.ClientErrorException; import javax.ws.rs.client.Client; import javax.ws.rs.client.WebTarget; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class GoogleClient { URL url; HttpURLConnection conn; String line, outputString = ""; BufferedReader reader; JSONParser parser = new JSONParser(); String distanceMeter; public GoogleClient(String destination) throws MalformedURLException, IOException, ParseException { url = new URL("https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vienna&destinations=" + destination); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = reader.readLine()) != null) { outputString += line; } // System.out.println(outputString); JSONObject responseObject = (JSONObject) new JSONParser().parse(outputString); String rows = responseObject.get("rows").toString(); // System.out.println("outputString=" + outputString); // System.out.println("rows=" + rows.substring(1, rows.length()-1)); JSONObject responseObjectRows = (JSONObject) new JSONParser().parse(rows.substring(1, rows.length() - 1)); String elements = responseObjectRows.get("elements").toString(); // System.out.println("elements=" + elements.substring(1, elements.length()-1)); JSONObject responseObjectElements = (JSONObject) new JSONParser() .parse(elements.substring(1, elements.length() - 1)); String distance = responseObjectElements.get("distance").toString(); // System.out.println("distance=" + distance.substring(1, distance.length()-1)); JSONObject responseObjectDistance = (JSONObject) new JSONParser().parse(distance); distanceMeter = responseObjectDistance.get("value").toString(); // System.out.println("value=" + distanceMeter); } public String getDistance() { return distanceMeter; } }