2011-08-29 87 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到我的商业应用程序的整个代码? –

相关问题