English 中文(简体)
JSON parsing of 谷歌地图
原标题:JSON parsing of Google Maps API in Android App

我试图用山角地图APIC来打上方向。 我很想创造ur,得到JSON的回复,然后审查旅行时间的回复。 在我创立了JSON目标之后,我难以浏览。 对我来说,这表明,我要么听了回答,要么把JSON的物体拿走。 我很想知道,你能否坐在我从网上的辅导员那里走到一起的手法和法典中。

该守则意在得到答复。 它环绕了审判/副渔获物,没有引起任何错误。

      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();

该守则旨在根据

        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");

I m 在第二组第二行捕获一名JSON例外。 谁能帮助解决这一问题? 或者说,获得同样数据的方法比较简单?

www.un.org/Depts/DGACM/index_spanish.htm EDIT:通过这里的第一个有益答案(从速),后半部分现为。

    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");

但是,它仍然 throw着一名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
               },
最佳回答

"routes" is an array, instead of using getJSONObject, use getJSONArray

"legs" is an array also.

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");
问题回答

I was also trying to use the Direction Api of Google in Android. So I made an open source project to help doing that. You can find it here:https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils

How it works, definitly simply:

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);
    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签