I'm trying to use the google maps API to fetch direction times. I'm hoping to create a url, get the JSON response, and then examine that response for travel duration. After I create the JSON object, I have trouble navigating it. To me, this indicates that I have either messed up getting the response or navigating the JSON object. I'd appreciate it if you could peek at the bits and pieces of code I have stitched together from tutorials around the web.
This code is intended to get the response. It's surrounded by a try/catch and it hasn't triggered any errors.
String stringUrl = <URL GOES HERE>;
URL url = new URL(stringUrl);
HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
{
BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192);
String strLine = null;
while ((strLine = input.readLine()) != null)
{
response.append(strLine);
}
input.close();
}
String jsonOutput = response.toString();
This code is intended to take that output and parse it into the final string, duration, as inspired by this stackoverflow answer for a similar question.
JSONObject jsonObject = 开发者_运维知识库new JSONObject(jsonOutput);
JSONObject routeObject = jsonObject.getJSONObject("routes");
JSONObject legsObject = routeObject.getJSONObject("legs");
JSONObject durationObject = legsObject.getJSONObject("duration");
String duration = durationObject.getString("text");
I'm catching a JSON exception on the second line of the second block. Can anyone help to fix this? Or perhaps suggest a simpler way to get the same data?
EDIT: thanks to the first helpful answer here (from aromero), the latter half now looks like:
JSONObject jsonObject = new JSONObject(responseText);
JSONArray routeObject = jsonObject.getJSONArray("routes");
JSONArray legsObject = routeObject.getJSONArray(2); ***error***
JSONObject durationObject = legsObject.getJSONObject(1);
String duration = durationObject.getString("text");
But it is still throwing a JSON exception, only now it's after the third line. I'm sure this is embarrassingly easy to fix, but I would really appreciate some help.
The relevant part of an example JSON file is shown below:
{
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 34.092810,
"lng" : -118.328860
},
"southwest" : {
"lat" : 33.995590,
"lng" : -118.446040
}
},
"copyrights" : "Map data ©2011 Google",
"legs" : [
{
"distance" : {
"text" : "12.9 mi",
"value" : 20807
},
"duration" : {
"text" : "27 mins",
"value" : 1619
},
"routes" is an array, instead of using getJSONObject
, use getJSONArray
"legs" is an array also.
JSONObject jsonObject = new JSONObject(responseText);
// routesArray contains ALL routes
JSONArray routesArray = jsonObject.getJSONArray("routes");
// Grab the first route
JSONObject route = routesArray.getJSONObject(0);
// Take all legs from the route
JSONArray legs = route.getJSONArray("legs");
// Grab first leg
JSONObject leg = legs.getJSONObject(0);
JSONObject durationObject = leg.getJSONObject("duration");
String duration = durationObject.getString("text");
I was also trying to use the Direction Api of Google in Android. So I made an open source project to help doing that. You can find it here:https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils
How it works, definitly simply:
public class MainActivity extends ActionBarActivity implements DCACallBack{
/**
* Get the Google Direction between mDevice location and the touched location using the Walk
* @param point
*/
private void getDirections(LatLng point) {
GDirectionsApiUtils.getDirection(this, startPoint, endPoint, GDirectionsApiUtils.MODE_WALKING);
}
/*
* The callback
* When the directions is built from the google server and parsed, this method is called and give you the expected direction
*/
@Override
public void onDirectionLoaded(List<GDirection> directions) {
// Display the direction or use the DirectionsApiUtils
for(GDirection direction:directions) {
Log.e("MainActivity", "onDirectionLoaded : Draw GDirections Called with path " + directions);
GDirectionsApiUtils.drawGDirection(direction, mMap);
}
}
精彩评论