3

我正在使用 google maps API 在某些点之间进行路由(向 google apis 询问这些道路路径)..

在大多数过程中运行良好,但有时在 JSONArray("routes") 上尝试 getJSONObject(0) 时出现 JSONException: Index 0 out of range [0..0) 导致我的应用程序丢失了一些道路;

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

这是我的一些东西..

在异步任务上

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 表单 URL

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());
        }
    }

失去一些道路

当 routeArray.getJSONObject(0) 得到 JSONException 时失败:索引 0 超出范围 [0..0) 导致我的应用程序丢失了一些类似上面的路径..

请帮我改哪一个:(

谢谢,我最好的问候..

编辑:打印我的 JSONObject 后,问题是 {"status":"OVER_QUERY_LIMIT","routes":[]} ..这就是为什么我的问题像随机的.so sad :(..

如何解决?

有什么技巧吗?

4

0 回答 0