问题
I am drawing route between 18 locations to draw the google map v2.When i start fetching routes it draws route but some times it is not drawing route and it gives JSONException :org.json.JSONException: Index 0 out of range [0..0) at org.json.JSONArray.get(JSONArray.java:263)
and also it gives "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();
}
Can any one suggest me what ll be the solutions for this? Thanks in advance.
回答1:
I faced the same problem before, and it was not because of Json. It was because of sending too much requests to google in a period. So I just slow down the frequency of sending post to Google, and never seen this message again.
回答2:
what @ohyes means is that you send multiple (too much) requests to google using your API key in too short of a time (for example, over 100 in a minute - just an example).
for that you are receiving the OVER_QUERY_LIMIT
and the empty JSON array
while you should consider changing the time between the requests, you should also verify that the array received in fact has any elements
try
{
// Tranform the string into a json object
final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
if(routeArray.size() > 0) {
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();
}
That would solve the exception being thrown, but it won't solve the fact that you are getting no result, probably because of the too much requests
EDIT:
The OVER_QUERY_LIMIT
thing happens (probably) because of too fast calls to getJSONFromUrl()
function. Please add the code that makes these calls so we could try and be more helpful on that.
Probably 500 ms delay is not enough (I'm assuming you're doing this on a background thread and not Sleeping
the UI thread as this is a bad habbit). try increasing the delay to something very high, such as a minute (60000 ms) and see if this helps. If it does, lower that number to the lowest one that works.
And add the code please.
来源:https://stackoverflow.com/questions/20700478/android-google-maps-api-2-got-jsonexceptionorg-json-jsonexception-index-0-out