Android谷歌地图API 2获得JSONException:索引0超出范围[0..0)

时间:2013-06-11 04:57:41

标签: android json google-maps google-polyline

我正在使用谷歌地图API在某些点之间路由(要求google apis获取这些道路路径)..

在多数进程中运行良好,但有时得到一个JSONException:索引0超出范围[0..0]而在JSONArray(“routes”)上尝试getJSONObject(0)时,我的应用程序会丢失一些路径;

我在测试此应用时使用带有WiFi连接的真实设备,我认为这不是问题..

这里有一些我的东西..

on Asynctask

protected void onProgressUpdate(String... progress) {
            super.onProgressUpdate();
            if (progress != null)
                drawPath(progress[0],color);
        }

        @Override
        protected String doInBackground(Void... params) {
            for (int i = 0; i < route_.size() - 1; i++) {
                String url = jsonParser.makeURL(route_.get(i).latitude,
                        route_.get(i).longitude, route_.get(i + 1).latitude,
                        route_.get(i + 1).longitude);
                //Log.i(TAG, i +" Asynctask : " + url);
                JSONParser jParser = new JSONParser();
                String json = jParser.getJSONFromUrl(url);
                publishProgress(json);
            }
            return "";
        }

网址制作者

public String makeURL(double sourcelat, double sourcelog, double destlat,
            double destlog) {
        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));
        urlString.append("&sensor=false&mode=driving&alternatives=false"); //Log.d(TAG, urlString.toString());
        return urlString.toString();
    }

获取JSON表单网址

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(TAG, "Buffer Error converting result " + e.toString());
        }
        return json;
    }



public 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(((lat / 1E5)), ((lng / 1E5)));
        poly.add(p);
    }
    return poly;
}

绘制路径

public void drawPath(String result , int color) {
        try {
            final JSONObject json = new JSONObject(result);  
            calcuDistTime(json);
            JSONArray routeArray = json.getJSONArray("routes");Log.d(TAG, "JSON Len " + routeArray.length());
            JSONObject routes;
            routes = routeArray.getJSONObject(0); <<< HERE THE PROBLEM org.json.JSONException: Index 0 out of range [0..0)
            JSONObject overviewPolylines = routes
                    .getJSONObject("overview_polyline");
            String encodedString = overviewPolylines.getString("points");
            List<LatLng> list = jsonParser.decodePoly(encodedString);
            for (int z = 0; z < list.size() - 1; z++) {
                LatLng src = list.get(z);
                LatLng dest = list.get(z + 1);
                map.addPolyline(new PolylineOptions()
                        .add(new LatLng(src.latitude, src.longitude),
                                new LatLng(dest.latitude, dest.longitude))
                        .width(8).color(color).geodesic(true));
                // Log.d(TAG, z + "DRAW POLYLINE : " + list.get(z).toString());
            }

        } catch (JSONException e) {
            Log.w(TAG, e.toString());
        }
    }

Loss some road paths

当routeArray.getJSONObject(0)得到JSONException时,

失败:索引0超出范围[0..0],导致我的应用程序丢失了一些上面的路径..

请帮助我,我必须改变哪一个:(

感谢, 我最诚挚的问候..

编辑:打印我的JSONObject之后问题是{“status”:“OVER_QUERY_LIMIT”,“routes”:[]} ..这就是为什么我的问题像随机的那样。所以悲伤:( ..

如何解决?

任何技巧?。

0 个答案:

没有答案