0

からポリラインを取得したいのですJSONObjectが、次のコードは機能しません。

JSONObject poly = steps.getJSONObject("polyline"); 

次のエラーが表示されます。

org.json.JSONException: ポリラインの値がありません

このコードは正常に動作します

JSONArray legs = routes.getJSONArray("legs");
JSONObject steps = legs.getJSONObject(0);
JSONObject endLoc = steps.getJSONObject("end_location");
JSONObject duration = steps.getJSONObject("duration");

着信 JSON ドキュメントは

{
  "duration": {
    "value": 363,
    "text": "6 mins"
  },
  "distance": {
    "value": 506,
    "text": "0.5 km"
  },
  "end_location": {
    "lng": 11.48949,
    "lat": 53.52159
  },
  "start_address": "PCH30, 19079 Banzkow, Germany",
  "end_address": "PCH30, 19079 Banzkow, Germany",
  "start_location": {
    "lng": 11.48189,
    "lat": 53.52114
  },
  "via_waypoint": [

  ],
  "steps": [
    {
      "html_instructions": "Head <b>east<\/b> on <b>PCH30<\/b><div style=\"font-size:0.9em\">Destination will be on the left<\/div>",
      "duration": {
        "value": 363,
        "text": "6 mins"
      },
      "distance": {
        "value": 506,
        "text": "0.5 km"
      },
      "end_location": {
        "lng": 11.48949,
        "lat": 53.52159
      },
      "polyline": {
        "points": "cjteIypaeAGqBKaEGaBC{@A_AM{EMwGMwFIsDC}@"
      },
      "travel_mode": "WALKING",
      "start_location": {
        "lng": 11.48189,
        "lat": 53.52114
      }
    }
  ]
}

そして、取得するための完全なコードJSONObject

StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json?");
urlString.append("origin=");// from
urlString.append(Double.toString((double) gpAlt.getLatitudeE6() / 1E6));
urlString.append(",");
urlString.append(Double.toString((double) gpAlt.getLongitudeE6() / 1E6));
urlString.append("&destination=");// to
urlString.append(Double.toString((double) gpNeu.getLatitudeE6() / 1E6));
urlString.append(",");
urlString.append(Double.toString((double) gpNeu.getLongitudeE6() / 1E6));
urlString.append("&mode=walking&sensor=true");
Log.d("xxx", "URL=" + urlString.toString());

urlConnection = null;
URL url = null;

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) 
{
    response += temp;
}

bReader.close();
inStream.close();
urlConnection.disconnect();

JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
JSONArray array = object.getJSONArray("routes");
JSONObject routes = array.getJSONObject(0);
String summary = routes.getString("summary");
JSONArray legs = routes.getJSONArray("legs");
JSONObject steps = legs.getJSONObject(0);
JSONObject poly = steps.getJSONObject("polyline");  
4

1 に答える 1

0

そこに表示されている JSON が正しくない場合を除き、それを取得するために必要なことは次のとおりです。

JSONArray steps = routes.getJSONArray("steps");
JSONObject obj = steps.getJSONObject(0);
JSONObject poly = obj.getJSONObject("polyline");  
于 2012-12-19T19:15:16.823 に答える