谷歌方向给出0值

时间:2015-12-05 11:37:03

标签: android google-maps google-api google-directory-api

嘿我已经使用谷歌方向来获得它的工作时间,但有时它会将0值返回到json数组路径。当我在返回一个值后测试一天。谷歌方向请求每天有任何限制吗?

这是我获得持续时间的功能



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

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

			String dura = "";
			try {

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

				// Output format


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


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

				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 = new 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 duration = steps.getJSONObject("duration");
				dura = duration.getString("text");

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

			return dura;
		}






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




1 个答案:

答案 0 :(得分:0)

检查你的JSON响应中是否也会得到类似的东西,除了持续时间为0。

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

这意味着您超出了Direction API用法的限制。请注意,对于标准版,每天只有2,500个免费路线请求。如果您需要更多请求,则需要支付额外费用。

查看有关Google Maps Directions API Usage Limits更多详细信息的官方文档。