Android Studio,Firebase W/System.err:org.json.JSONException:Index 0超出范围[0..0]

Android Studio,Firebase W/System.err:org.json.JSONException:Index 0超出范围[0..0],android,json,google-maps,firebase,Android,Json,Google Maps,Firebase,现在我正在校园里跟踪公交车 正如你们看到的截图。现在有两辆巴士。所以当点击巴士图标时。它会显示距离和到达时间,但不会显示任何内容 我从Firebase检索位置 我正在使用java、Android studio和Firebase W/System.err: org.json.JSONException: Index 0 out of range [0..0) logcat中的错误显示 该错误表示您拥有的“routes”数组的长度为0。因此,数组中没有项目,并且您试图访问索引1处的对象,因此它

现在我正在校园里跟踪公交车

正如你们看到的截图。现在有两辆巴士。所以当点击巴士图标时。它会显示距离和到达时间,但不会显示任何内容

我从Firebase检索位置

我正在使用java、Android studio和Firebase

W/System.err: org.json.JSONException: Index 0 out of range [0..0)

logcat中的错误显示


该错误表示您拥有的“routes”数组的长度为0。因此,数组中没有项目,并且您试图访问索引1处的对象,因此它抛出一个错误,即数组为空,因此数组中没有索引1


尝试将整个JSON主体记录到控制台,并检查“route”键是否包含对象。

看起来您的routes数组是空的。您可以发布从directions api获得的响应吗?
mDriver = mMap.addMarker(new MarkerOptions()
.position(new LatLng(location.latitude, location.longitude))
.title(rider.getEmail())
.snippet("DISTANCE : " +getDistanceInfo(currentUser,driver)+" KM ")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.bus)));

private double getDistanceInfo(Location currentUser, Location driver) {
            StringBuilder stringBuilder = new StringBuilder();


            double dist = 0.00 ;


            String latDriver = Double.toString(driver.getLatitude());


            String lngDriver = Double.toString(driver.getLongitude());



            String latCurrent = Double.toString(currentUser.getLatitude());
            String lngCurrent = Double.toString(currentUser.getLatitude());


            try {

                String url = "http://maps.googleapis.com/maps/api/directions/json?"+"&origin="+latDriver + "," + lngDriver  + "&destination=" +latCurrent + "," + lngCurrent+ "&mode=driving&sensor=false+"+"key="+getResources().getString(R.string.google_browser_key);


                HttpPost httppost = new HttpPost(url);

                HttpClient client = new DefaultHttpClient();
                HttpResponse response;
                stringBuilder = new StringBuilder();


                response = client.execute(httppost);
                HttpEntity entity = response.getEntity();
                InputStream stream = entity.getContent();
                int b;
                while ((b = stream.read()) != -1) {
                    stringBuilder.append((char) b);
                }
            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }

            JSONObject jsonObject;


            try {


                    jsonObject = new JSONObject(stringBuilder.toString());

                    JSONArray array = jsonObject.getJSONArray("routes");

                    JSONObject routes = array.getJSONObject(0);

                    JSONArray legs = routes.getJSONArray("legs");

                    JSONObject steps = legs.getJSONObject(0);

                    JSONObject distance= steps.getJSONObject("distance");

                    Log.i("Distance", distance.toString());
                    dist = Double.parseDouble(distance.getString("text").replaceAll("[^\\.0123456789]", ""));



            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return (dist);
        }