2015-12-05 20 views
0

嘿我已经使用谷歌方向获得持续时间它的工作,但有时它返回0值到JSON数组路径。当我测试一天后返回一个值。每天的谷歌方向请求是否有任何限制?谷歌方向给出0值

,这里是我的功能得到持续

public String getDistanceInfo(LatLng origin, LatLng dest) { 
 
\t \t \t StringBuilder stringBuilder = new StringBuilder(); 
 
\t \t \t String str_origin = "origin=" + origin.latitude + "," + origin.longitude; 
 

 
\t \t \t // Destination of route 
 
\t \t \t String str_dest = "destination=" + dest.latitude + "," + dest.longitude; 
 

 
\t \t \t String dura = ""; 
 
\t \t \t try { 
 

 
\t \t \t \t String sensor = "sensor=false"; 
 
\t \t \t \t String output = "json"; 
 
\t \t \t \t String mode = "mode=walking"; 
 
\t \t \t \t String parameters = str_origin + "&" + str_dest + "&" + sensor + "&" + mode; 
 

 
\t \t \t \t // Output format 
 

 

 
\t \t \t \t // Building the url to the web service 
 
\t \t \t \t String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; 
 

 

 
\t \t \t \t //String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + str_origin + "," + str_dest + "&destination=" + destinationAddress + "&mode=driving&sensor=false"; 
 

 
\t \t \t \t HttpPost httppost = new HttpPost(url); 
 

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

 

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

 
\t \t \t JSONObject jsonObject = new JSONObject(); 
 
\t \t \t try { 
 

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

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

 
\t \t \t \t JSONObject routes = array.getJSONObject(0); 
 

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

 
\t \t \t \t JSONObject steps = legs.getJSONObject(0); 
 

 
\t \t \t \t JSONObject duration = steps.getJSONObject("duration"); 
 
\t \t \t \t dura = duration.getString("text"); 
 

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

 
\t \t \t return dura; 
 
\t \t }

org.json.JSONException: Index 0 out of range [0..0) 
 
org.json.JSONArray.get(JSONArray.java:282) 
 
org.json.JSONArray.getJSONObject(JSONArray.java:510)

+0

如果它给出一个零值..原因可能是没有可能的路径 – ManishNegi

+0

@ManishNegi它给0没有价值的jsonArray路线 – Mariam

+0

你能粘贴消息吗? – ManishNegi

回答

0

检查,看看你是否也在你的JSON响应得到这样的事情太除了0持续时间。

{"status":"OVER_QUERY_LIMIT","routes":[]}. 

这意味着您超出了方向API使用的限制。请注意,对于标准版本,每天只有2,500个免费指导请求可用。如果您需要更多请求,则需支付相关费用。

查看Google Maps Directions API Usage Limits的官方文档更多细节。