2013-10-26 53 views
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

請幫我出什麼問題。

+0

在大多數情況下,您只需要通過異常堆棧跟蹤,您可以輕鬆找出問題所在。如果您無法做到這一點,請在此處發佈跟蹤。 –

回答

0

首先,不硬編碼任何位置(如0)從數組中獲得。 Bcs,數組可能爲空

這就是你的情況。你的其中一個arraylegsJSONArray爲空但你試圖獲得它們的第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"); 
    } 

} 
相關問題