title: How to get duration and distance in JSON object return by Google?
body: <p>I want to get the distance and duration for example in this 
  <a href="https://maps.googleapis.com/maps/api/directions/json?key=API_Key&amp;sensor=true&amp;language=en&amp;origin=6.035507,116.1213559&amp;destination=5.9699316,116.0665816" rel="nofollow noreferrer">https://maps.googleapis.com/maps/api/directions/json?key=API_Key&amp;sensor=true&amp;language=en&amp;origin=6.035507,116.1213559&amp;destination=5.9699316,116.0665816</a>. I have already get the route in between these 2 locations. But I don't know how to get the distance and duration displayed. Need some help. I already knew the others already posted this question, but it seems cannot work with my code due to I am consider a rookie in Android. 
  Below is my code: </p>
  
  <pre><code>private void drawRoute(final LatLng yourLocation, final Request request) {
  
      //clear all polyline
      if (polyline != null)
          polyline.remove();
  
      if (request.getAddress() != null &amp;&amp; !request.getAddress().isEmpty()) {
          mService.getGeoCode(request.getAddress()).enqueue(new Callback&lt;String&gt;() {
              @Override
              public void onResponse(Call&lt;String&gt; call, Response&lt;String&gt; response) {
                  try {
                      JSONObject jsonObject = new JSONObject(response.body().toString());
  
                      String lat = ((JSONArray) jsonObject.get("results"))
                              .getJSONObject(0)
                              .getJSONObject("geometry")
                              .getJSONObject("location")
                              .get("lat").toString();
  
                       String lng = ((JSONArray) jsonObject.get("results"))
                              .getJSONObject(0)
                              .getJSONObject("geometry")
                              .getJSONObject("location")
                              .get("lng").toString();
  
                      final LatLng orderLocation = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
  
                      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.deliverybox);
                      bitmap = Common.scaleBitmap(bitmap, 70, 70);
  
                      MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
                              .title("Order of " + Common.currentRequest.getPhone())
                              .position(orderLocation);
  
                      mMap.addMarker(marker);
  
                      //draw route
  
                      mService.getDirections(yourLocation.latitude + "," + yourLocation.longitude,
                              orderLocation.latitude + "," + orderLocation.longitude)
                              .enqueue(new Callback&lt;String&gt;() {
                                  @Override
                                  public void onResponse(Call&lt;String&gt; call, Response&lt;String&gt; response) {
  
                                      new ParserTask().execute(response.body().toString());
  
                                  }
  
                                  @Override
                                  public void onFailure(Call&lt;String&gt; call, Throwable t) {
  
                                  }
                              });
  
                  } catch (JSONException e) {
  
                      e.printStackTrace();
                  }
              }
  
              @Override
              public void onFailure(Call&lt;String&gt; call, Throwable t) {
  
              }
          });
      } else {
          if (request.getLatLng() != null &amp;&amp; !request.getLatLng().isEmpty()) {
              String[] latLng = request.getLatLng().split(",");
              LatLng orderLocation = new LatLng(Double.parseDouble(latLng[0]), Double.parseDouble(latLng[1]));
              Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.deliverybox);
              bitmap = Common.scaleBitmap(bitmap, 70, 70);
  
              MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
                      .title("Order of " + Common.currentRequest.getPhone())
                      .position(orderLocation);
  
              mMap.addMarker(marker);
              mService.getDirections(mLastLocation.getLatitude() + "," + mLastLocation.getLongitude(),
                      orderLocation.latitude + "," + orderLocation.longitude)
                      .enqueue(new Callback&lt;String&gt;() {
                          @Override
                          public void onResponse(Call&lt;String&gt; call, Response&lt;String&gt; response) {
                              new ParserTask().execute(response.body().toString());
                          }
  
                          @Override
                          public void onFailure(Call&lt;String&gt; call, Throwable t) {
  
                          }
                      });
          }
      }
  }
  </code></pre>
  
  <p>And here is where I parser the JSON Object returned by Google</p>
  
  <pre><code>private class ParserTask extends AsyncTask&lt;String, Integer, 
  List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt;&gt; {
  
      ProgressDialog mDialog = new ProgressDialog(TrackingOrder.this);
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          mDialog.setMessage("Please waiting...");
          mDialog.show();
      }
  
      @Override
      protected List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; doInBackground(String... strings) {
  
          JSONObject jsonObject;
          List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; routes = null;
          try {
              jsonObject = new JSONObject(strings[0]);
              DirectionJSONParser parser = new DirectionJSONParser();
  
              routes = parser.parse(jsonObject);
  
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
          return routes;
  
      }
  
      @Override
      protected void onPostExecute(List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; lists) {
          mDialog.dismiss();
  
          ArrayList points = null;
          PolylineOptions lineOptions = null;
  
          for (int i = 0; i &lt; lists.size(); i++) {
  
              points = new ArrayList();
              lineOptions = new PolylineOptions();
  
              List&lt;HashMap&lt;String, String&gt;&gt; path = lists.get(i);
  
              for (int j = 0; j &lt; path.size(); j++) {
  
                  HashMap&lt;String, String&gt; point = path.get(j);
  
                  double lat = Double.parseDouble(point.get("lat"));
                  double lng = Double.parseDouble(point.get("lng"));
  
                  LatLng position = new LatLng(lat, lng);
  
                  points.add(position);
              }
  
              lineOptions.addAll(points);
              lineOptions.width(8);
              lineOptions.color(Color.RED);
              lineOptions.geodesic(true);
          }
  
          mMap.addPolyline(lineOptions);
      }
  }
  </code></pre>
  
  <p>And lastly here is my DirectionJsonParser.java </p>
  
  <pre><code>public class DirectionJSONParser {
  
  /**
   * Receives a JSONObject and returns a list of lists containing latitude and longitude
   */
  public List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; parse(JSONObject jObject) {
  
      List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; routes = new ArrayList&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt;();
      JSONArray jRoutes = null;
      JSONArray jLegs = null;
      JSONArray jSteps = null;
  
      try {
  
          jRoutes = jObject.getJSONArray("routes");
  
          /** Traversing all routes */
          for (int i = 0; i &lt; jRoutes.length(); i++) {
              jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");
              List path = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
              /** Traversing all legs */
              for (int j = 0; j &lt; jLegs.length(); j++) {
                  jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps");
  
                  /** Traversing all steps */
                  for (int k = 0; k &lt; jSteps.length(); k++) {
                      String polyline = "";
                      polyline = (String) ((JSONObject) ((JSONObject) jSteps.get(k)).get("polyline")).get("points");
                      List list = decodePoly(polyline);
  
                      /** Traversing all points */
                      for (int l = 0; l &lt; list.size(); l++) {
                          HashMap&lt;String, String&gt; hm = new HashMap&lt;String, String&gt;();
                          hm.put("lat", Double.toString(((LatLng) list.get(l)).latitude));
                          hm.put("lng", Double.toString(((LatLng) list.get(l)).longitude));
                          path.add(hm);
                      }
                  }
                  routes.add(path);
              }
          }
  
      } catch (JSONException e) {
          e.printStackTrace();
      } catch (Exception e) {
      }
  
      return routes;
  }
  
  /**
   * Method to decode polyline points
   * Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
   */
  private List decodePoly(String encoded) {
      List poly = new ArrayList();
      int index = 0, len = encoded.length();
      int lat = 0, lng = 0;
  
      while (index &lt; len) {
          int b, shift = 0, result = 0;
          do {
              b = encoded.charAt(index++) - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          int dlat = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lat += dlat;
  
          shift = 0;
          result = 0;
          do {
              b = encoded.charAt(index++) - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          int dlng = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lng += dlng;
  
          LatLng p = new LatLng((((double) lat / 1E5)),
                  (((double) lng / 1E5)));
          poly.add(p);
      }
  
      return poly;
  }
  }
  </code></pre>