0
我想要找出兩個經度和緯度之間的駕駛距離。 這是我的代碼找到兩個地理區域之間的駕駛距離的例外
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)
這是我的堆棧跟蹤
Ljava.lang.StackTraceElement;@41019be8
請幫我出什麼問題。
在大多數情況下,您只需要通過異常堆棧跟蹤,您可以輕鬆找出問題所在。如果您無法做到這一點,請在此處發佈跟蹤。 –