Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON parsing of Google Maps API in Android App

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
               },
like image 590
user714403 Avatar asked Aug 29 '11 23:08

user714403


1 Answers

"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");
like image 138
aromero Avatar answered Nov 16 '22 02:11

aromero