您好我在我的logcat中收到以下错误,我不知道是什么问题:
(我添加了日志声明)
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)
这是我的代码:
String distance = getDistance(currentlatitude,currentlongitude,Double.parseDouble(latitude),Double.parseDouble(longitude));
然后
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) + "&destinations=" + Double.toString(lat2) + "," + Double.toString(lon2) + "&mode=driving&language=en-EN&units=metric&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 < 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 < 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;
}
感谢您的帮助!
答案 0 :(得分:0)
我发现了我的问题。我把我的日志语句看到我的输入流结果,发现我的原始地址(GPS坐标)是0,0。因此,距离没有价值。
try {
InputStream is = null;
httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
jsonn = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(jsonn);
Log.d("results:", jsonn); //To See Results
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
JSONArray rowsArray = jObj.getJSONArray("rows");
JSONObject rows = rowsArray.getJSONObject(0);
//JSONArray rows = rootObject.getJSONArray("rows"); // Get all JSONArray rows
JSONArray elementsArray = rows.getJSONArray("elements"); // Get all elements for each row as an array
JSONObject newDisTimeOb = elementsArray.getJSONObject(0);
JSONObject distOb = newDisTimeOb.getJSONObject("distance");
getdistance = String.valueOf(distOb.getDouble("value") / 1000);
Log.d("Distance text&value :", distOb.getString("text") + getdistance);
}