我想知道两个纬度和经度之间的行驶距离。 这是我的代码
private String GetDistance(LatLng origin, LatLng dest) { // Origin of route String str_origin = "origin=" + origin.latitude + "," + origin.longitude; // Destination of route String str_dest = "destination=" + dest.latitude + "," + dest.longitude; // Sensor enabled String sensor = "sensor=false"; // Building the parameters to the web service String parameters = str_origin + "&" + str_dest + "&" + sensor; // Output format String output = "json"; // Building the url to the web service String urlString = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; // get the JSON And parse it to get the directions data. HttpURLConnection urlConnection = null; URL url = null; try { url = new URL(urlString.toString()); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.connect(); InputStream inStream = urlConnection.getInputStream(); BufferedReader bReader = new BufferedReader(new InputStreamReader( inStream)); String temp,response = ""; while ((temp = bReader.readLine()) != null) { // Parse data response += temp; } // Close the reader, stream & connection bReader.close(); inStream.close(); urlConnection.disconnect(); // Sort out JSONresponse // JSONObject object = (JSONObject) new JSONTokener(response) // .nextValue(); JSONObject object = new JSONObject(response); JSONArray array = object.getJSONArray("routes"); // Log.d("JSON","array: "+array.toString()); // Routes is a combination of objects and arrays JSONObject routes = array.getJSONObject(0); // Log.d("JSON","routes: "+routes.toString()); String summary = routes.getString("summary"); Log.d("JSON","summary: "+summary); JSONArray legs = routes.getJSONArray("legs"); // Log.d("JSON","legs: "+legs.toString()); JSONObject steps = legs.getJSONObject(0); // Log.d("JSON","steps: "+steps.toString()); JSONObject distance = steps.getJSONObject("distance"); // Log.d("JSON","distance: "+distance.toString()); sDistance = distance.getString("text"); iDistance = distance.getInt("value"); } catch (Exception e) { // TODO: handle exception return e.toString(); } return sDistance; }
我正在获得例外
org.json.JSONException:Index 0 out of range [0..0)
这是我的stacktrace
Ljava.lang.StackTraceElement;@41019be8
请帮我解决问题所在。
答案 0 :(得分:0)
首先,不要硬编码任何位置(如0)从数组中获取。 Bcs,数组可能为空。
这就是你的情况。您的array
或legs
JSONArray中的一个是空的,但您正试图获得它们的第0个位置。因此,它会使索引超出范围异常。
从数组中获取值更好地用于循环。示例代码段是:
Log.v("array-length--", ""+array.length());
for(int i=0; i < array.length();i++)
{
// Routes is a combination of objects and arrays
JSONObject routes = array.getJSONObject(i);
// Log.d("JSON","routes: "+routes.toString());
String summary = routes.getString("summary");
Log.d("JSON","summary: "+summary);
JSONArray legs = routes.getJSONArray("legs");
// Log.d("JSON","legs: "+legs.toString());
Log.v("legs-length--", ""+legs.length());
for(int j=0; j < legs.length(); j++)
{
JSONObject steps = legs.getJSONObject(j);
// Log.d("JSON","steps: "+steps.toString());
JSONObject distance = steps.getJSONObject("distance");
// Log.d("JSON","distance: "+distance.toString());
sDistance = distance.getString("text");
iDistance = distance.getInt("value");
}
}