旅行模式骑自行车没有结果

时间:2014-01-29 16:58:34

标签: android directions

如果我尝试使用旅行模式骑自行车请求路径我得到org.json.JSONException:索引0超出范围[0..0)

这是ulr制造商:

private String makeURL(double sourcelat, double sourcelog, double destlat, double destlog, String travelMode){
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    urlString.append("?origin=");// from
    urlString.append(Double.toString(sourcelat));
    urlString.append(",");
    urlString
            .append(Double.toString(sourcelog));
    urlString.append("&destination=");// to
    urlString
            .append(Double.toString(destlat));
    urlString.append(",");
    urlString.append(Double.toString(destlog));
    //"&sensor=false&mode=driving&alternatives=true"
    Log.d(TAG_LOG, "Travel mode:" + travelMode);
    urlString.append("&sensor=false&mode=" + travelMode + "&alternatives=true");
    return urlString.toString();
}

错误在于:

final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");

为什么呢? :(

1 个答案:

答案 0 :(得分:0)

发生此错误是因为routeArray不包含任何元素。尝试在浏览器中输入您的URL并查看JSON响应实际包含的内容。