本文介绍了Android的谷歌地图API 2得到JSONException:org.json.JSONException:指数0超出范围[0..0)在org.json.JSONArray.get(JSONArray.java:263)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画的路线之间的18个地点绘制谷歌地图v2.When我开始获取路线它绘制的路线,但有时它不是绘制的路线,这让 JSONException:org.json.JSONException:指数0超出范围[0..0)在org.json.JSONArray.get(JSONArray.java:263)

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)

和还它给ERROR_MESSAGE:你已经超过了你的日常请求限额为这个API。路线:[]状态:OVER_QUERY_LIMIT

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.

推荐答案

我之前遇到过同样的问题,这是因为的JSON不是。这是因为发送太多的请求在一段时间内给Google。所以我就慢下来后发送给谷歌的频率,也再没有见过此消息。

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.

这篇关于Android的谷歌地图API 2得到JSONException:org.json.JSONException:指数0超出范围[0..0)在org.json.JSONArray.get(JSONArray.java:263)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:15