title: Google API Distance Matrix, no value for distance
body: <p>Hi I receive the following error in my logcat and I do not know what is the problem:</p>
  
  <p>(I have added log statements)</p>
  
  <pre><code>D/loop!﹕ rows
  D/loop!﹕ elements
  W/System.err﹕ org.json.JSONException: No value for distance
  W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:355)
  W/System.err﹕ at org.json.JSONObject.getJSONObject(JSONObject.java:574)
  W/System.err﹕ at info.tabsswipe.Results.getDistance(Results.java:279)
  </code></pre>
  
  <p>Here is my code:</p>
  
  <p>String distance = getDistance(currentlatitude, currentlongitude, Double.parseDouble(latitude), Double.parseDouble(longitude));</p>
  
  <p>and then</p>
  
  <pre><code>private String getDistance(Double lat1, Double lon1, double lat2, double lon2) {
  
      String getdistance = "";
      String url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + Double.toString(lat1) + "," + Double.toString(lon1) + "&amp;destinations=" + Double.toString(lat2) + "," + Double.toString(lon2) + "&amp;mode=driving&amp;language=en-EN&amp;units=metric&amp;key=somethinghere";
      HttpGet httpGet = new HttpGet(url);
      HttpClient httpClient = new DefaultHttpClient();
      HttpResponse httpResponse;
  
      try {
          httpResponse = httpClient.execute(httpGet);
          HttpEntity httpEntity = httpResponse.getEntity();
          String line = EntityUtils.toString(httpEntity);
  
          JSONObject rootObject = new JSONObject(line);
          JSONArray rows = rootObject.getJSONArray("rows"); // Get all JSONArray rows
  
          for (int i = 0; i &lt; rows.length(); i++) { // Loop over each each row
              Log.d("loop!", "rows");
              JSONObject row = rows.getJSONObject(i); // Get row object
              JSONArray elements = row.getJSONArray("elements"); // Get all elements for each row as an array
              for (int j = 0; j &lt; elements.length(); j++) { // Iterate each element in the elements array
                  Log.d("loop!", "elements");
                  JSONObject element = elements.getJSONObject(j); // Get the element object
                  JSONObject distance = element.getJSONObject("distance"); // Get distance sub object
                  getdistance = String.valueOf(distance.getInt("value")/1000);
              }
  
          }
  
      } catch (JSONException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      return getdistance;
  }
  </code></pre>
  
  <p>Thanks for your help!</p>