Android Google Maps API 2 hat JSONException: org.json.JSONException: Index 0 außerhalb des Bereichs [0..0) bei org.json.JSONArray.get (JSONArray.java:263)

Ich zeichne eine Route zwischen 18 Orten, um die Google Map v2 zu zeichnen. Wenn ich anfange, Routen abzurufen, zeichnet es eine Route, aber manchmal zeichnet es keine Route und es gibtJSONException :org.json.JSONException: Index 0 out of range [0..0) at org.json.JSONArray.get(JSONArray.java:263)

und es gibt auch"error_message" : "You have exceeded your daily request quota for this API." "routes" : [] "status" : "OVER_QUERY_LIMIT"

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {
    }

    public String getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            json = sb.toString();
            is.close();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        Log.d("JSON_RUTA", json);
        return json;

    }
}




private String makeURL(double sourcelat, double sourcelog, double destlat,
        double destlog, String mode)
{
    StringBuilder urlString = new StringBuilder();

    if (mode == null)
        mode = "driving";

    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));
    urlString.append("&sensor=false&mode=" + mode + "&units=metric");
    return urlString.toString();
}




private List<LatLng> decodePoly(String encoded)
{

    List<LatLng> poly = new ArrayList<LatLng>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;

    while (index < len)
    {
        int b, shift = 0, result = 0;
        do
        {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;

        shift = 0;
        result = 0;
        do
        {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;

        LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5)));
        poly.add(p);
    }

    return poly;
}


try
    {
        // Tranform the string into a json object
        final JSONObject json = new JSONObject(result);
        JSONArray routeArray = json.getJSONArray("routes");
        JSONObject routes = routeArray.getJSONObject(0);
        JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
        encodedString = overviewPolylines.getString("points");

               List<LatLng> list = decodePoly(encodedString);   
    } catch (JSONException e)
    {
        e.printStackTrace();
    }




for (int z = 0; z < list.size() - 1; z++)
{
 LatLng src = list.get(z);
 LatLng dest = list.get(z + 1);

 Polyline line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
 new LatLng(dest.latitude, dest.longitude)).width(4)
.color(Color.BLUE).geodesic(true));
 line.isVisible();
 }

Kann mir jemand eine Lösung vorschlagen? Danke im Voraus.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage