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)
如果它給出一個零值..原因可能是沒有可能的路徑 – ManishNegi
@ManishNegi它給0沒有價值的jsonArray路線 – Mariam
你能粘貼消息嗎? – ManishNegi