2011-08-29 78 views
8

我正在嘗試使用google maps API來獲取方向時間。我希望創建一個url,獲取JSON響應,然後檢查該響應的旅行時間。創建JSON對象後,我無法導航它。對我來說,這表明我搞砸了獲取響應或瀏覽JSON對象。如果你能夠從網上的教程中看到我編寫的代碼片段,我會很感激。Android應用程序中的Google Maps API的JSON解析

此代碼旨在獲得響應。它被try/catch包圍,並沒有引發任何錯誤。

 String stringUrl = <URL GOES HERE>; 

     URL url = new URL(stringUrl); 
     HttpURLConnection httpconn = (HttpURLConnection)url.openConnection(); 
     if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) 
     { 
      BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192); 
      String strLine = null; 
      while ((strLine = input.readLine()) != null) 
      { 
       response.append(strLine); 
      } 
      input.close(); 
     } 
     String jsonOutput = response.toString(); 

此代碼旨在採取輸出並將其解析爲最後一個字符串,持續時間,作爲用於類似的問題啓發this stackoverflow answer

 JSONObject jsonObject = new JSONObject(jsonOutput); 
     JSONObject routeObject = jsonObject.getJSONObject("routes"); 
     JSONObject legsObject = routeObject.getJSONObject("legs"); 
     JSONObject durationObject = legsObject.getJSONObject("duration"); 
     String duration = durationObject.getString("text"); 

我在第二個塊的第二行捕獲JSON異常。誰能幫助解決這個問題嗎?或者也許建議一個更簡單的方法來獲取相同的數據?

編輯:多虧了第一有用的答案在這裏(從aromero),下半年現在看起來像:

JSONObject jsonObject = new JSONObject(responseText); 
    JSONArray routeObject = jsonObject.getJSONArray("routes"); 
    JSONArray legsObject = routeObject.getJSONArray(2); ***error*** 
    JSONObject durationObject = legsObject.getJSONObject(1); 
     String duration = durationObject.getString("text"); 

但它仍然拋出一個JSON例外,只是現在它的第三行之後。我確信這很容易解決,但我真的很感謝一些幫助。

示例JSON文件的相關部分如下所示:

{ 
    "routes" : [ 
     { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 34.092810, 
       "lng" : -118.328860 
      }, 
      "southwest" : { 
       "lat" : 33.995590, 
       "lng" : -118.446040 
      } 
     }, 
     "copyrights" : "Map data ©2011 Google", 
     "legs" : [ 
      { 
       "distance" : { 
        "text" : "12.9 mi", 
        "value" : 20807 
       }, 
       "duration" : { 
        "text" : "27 mins", 
        "value" : 1619 
       }, 

回答

14

「路線」是一個數組,而不是使用getJSONObject,使用getJSONArray

「腿」是一個數組也。

JSONObject jsonObject = new JSONObject(responseText); 

// routesArray contains ALL routes 
JSONArray routesArray = jsonObject.getJSONArray("routes"); 
// Grab the first route 
JSONObject route = routesArray.getJSONObject(0); 
// Take all legs from the route 
JSONArray legs = route.getJSONArray("legs"); 
// Grab first leg 
JSONObject leg = legs.getJSONObject(0); 

JSONObject durationObject = leg.getJSONObject("duration"); 
String duration = durationObject.getString("text"); 
+0

我想你已經確定什麼,我做錯了。仍然熨平了具體情況。當我修復它時,我會回覆,除非有人打我。 – user714403

+0

路由是一個數組,我認爲谷歌正在返回給你一個可能的路由列表。所以你需要抓住其中的一個,或者對他們做一些事情。你想做什麼? – aromero

+0

太棒了!感謝您對原始答案的編輯,我非常欣賞握着的手。我真的不明白JSON的語法和間隔,你已經很清楚了。非常感謝! – user714403

2

我也試圖在Android中使用谷歌的方向Api。 所以我做了一個開源項目來幫助做到這一點。 你可以在這裏找到:https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils

它是如何工作的,definitly簡單:

public class MainActivity extends ActionBarActivity implements DCACallBack{ 
/** 
* Get the Google Direction between mDevice location and the touched location using the  Walk 
* @param point 
*/ 
private void getDirections(LatLng point) { 
    GDirectionsApiUtils.getDirection(this, startPoint, endPoint, GDirectionsApiUtils.MODE_WALKING); 
} 

/* 
* The callback 
* When the directions is built from the google server and parsed, this method is called and give you the expected direction 
*/ 
@Override 
public void onDirectionLoaded(List<GDirection> directions) {   
    // Display the direction or use the DirectionsApiUtils 
    for(GDirection direction:directions) { 
     Log.e("MainActivity", "onDirectionLoaded : Draw GDirections Called with path " + directions); 
     GDirectionsApiUtils.drawGDirection(direction, mMap); 
    } 
} 
+0

我有可能實現從Github到我的商業應用程序的整個代碼? –

相關問題