我在18个地点之间绘制路线以绘制谷歌地图v2。当我开始提取路线时,它会绘制路线,但有时它不是绘制路线而且它会给出JSONException :org.json.JSONException: Index 0 out of range [0..0) at org.json.JSONArray.get(JSONArray.java:263)
并且还提供了"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();
}
任何人都可以建议我解决这个问题吗? 提前谢谢。
答案 0 :(得分:1)
之前我遇到过同样的问题,这不是因为Json。这是因为在一段时间内向谷歌发送了太多的请求。所以我只是放慢了向Google发送帖子的频率,再也没有看过这条消息了。
答案 1 :(得分:0)
为此您收到OVER_QUERY_LIMIT
和空JSON数组
虽然您应该考虑更改请求之间的时间,但您还应该验证接收到的数组实际上是否有任何元素
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();
}
这样可以解决被抛出的异常,但它无法解决您没有得到任何结果的事实,可能是因为请求太多
修改强>
由于对OVER_QUERY_LIMIT
函数的调用过快,getJSONFromUrl()
事件(可能)发生了。请添加进行这些调用的代码,以便我们可以尝试对此更有帮助。
可能500毫秒的延迟是不够的(我假设你在后台线程上这样做而不是Sleeping
UI线程,因为这是一个糟糕的习惯)。尝试将延迟增加到非常高的程度,例如一分钟(60000毫秒),看看是否有帮助。如果是,请将该数字降低到有效的最低数字。
请添加代码。