现在我正在校园校车。
你可以看到截图。现在有两辆公共汽车。所以当点击图标总线。它会显示距离和持续时间到达时间,但它没有显示任何内容。
我从Firebase检索位置。
我正在使用java,Android studio和Firebase。
W/System.err: org.json.JSONException: Index 0 out of range [0..0)
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);
}
logcat
中显示错误答案 0 :(得分:0)
错误表明你拥有的“路由”数组的长度为0.所以数组中没有项目,你试图访问索引1处的对象,所以它抛出一个错误,表明数组是空的,所以那里数组中没有索引1。
尝试将整个JSON主体记录到控制台,并检查“route”键是否包含对象。