2014-10-31 37 views
0

我在嘗試下載並顯示折線。正在下載折線信息

我有這樣的任務異步設置:

private class taskGetPolyline extends AsyncTask<Void, Void, Void> { 

    ProgressDialog dialog; 
    StringBuilder urlString; 
    String response; 
    int i; 

    private taskGetPolyline(int i) { 
     this.i = i; //passes in id of closest marker 
    } 

    @Override protected void onPreExecute() { 
     super.onPreExecute(); 

     dialog = new ProgressDialog(ContentActivity.this); 
     dialog.setTitle(getResources().getText(
       R.string.progress_download_route)); 
     dialog.setMessage(getResources().getText(
       R.string.progress_please_wait)); dialog.setCancelable(false); 
       dialog.setCanceledOnTouchOutside(false); dialog.show(); 

     urlString = new StringBuilder(); urlString 
       .append("http://maps.googleapis.com/maps/api/directions/json"); 
     urlString.append("?origin="); 
     urlString.append(LOCATION_CURRENT.getLatitude()); urlString.append(","); 
     urlString.append(LOCATION_CURRENT.getLongitude()); 
     urlString.append("&destination="); 

     urlString.append(arrayLocation[i].latitude); 
     urlString.append(","); 
     urlString.append(arrayLocation[i].longitude); 
     urlString.append("&sensor=false"); 

     Log.i("DEBUG","link: " + urlString.toString()); 

    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     try { 
      HttpClient Client = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(urlString.toString()); 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      response = Client.execute(httpGet, responseHandler); 

      Log.i("DEBUG", "response = " + response); 
     } catch (Exception e) { 
      Log.i("Exception", e.getMessage()); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 

     JSONObject jsonResponse; 
     try { 
      jsonResponse = new JSONObject(response); 
      JSONObject jsonOverviewPolylines = jsonResponse 
        .getJSONObject("overview_polyline"); 

      PolylineOptions polylineOptions = new PolylineOptions() 
        .addAll(decodePolyLine(jsonOverviewPolylines.toString())); 
      currentPolyline = map.addPolyline(polylineOptions); 



     } catch (JSONException e) { 

      e.printStackTrace(); 
     } 
     dialog.dismiss(); 
    } 

} 

但是當我運行這段代碼我得到logcat的警告:

10-31 14:48:43.744: W/System.err(10724): org.json.JSONException: No value for overview_polyline 

日誌裏顯示整個鏈接,這很好。這裏的例子:不顯示,直到在日誌中結束 http://maps.googleapis.com/maps/api/directions/json?origin=46.14928015,16.00907806&destination=45.831534,16.02944&sensor=false

響應:

10-31 14:55:35.744: I/DEBUG(11671): response = { 
10-31 14:55:35.744: I/DEBUG(11671): "routes" : [ 
10-31 14:55:35.744: I/DEBUG(11671):  { 
10-31 14:55:35.744: I/DEBUG(11671):   "bounds" : { 
10-31 14:55:35.744: I/DEBUG(11671):    "northeast" : { 
10-31 14:55:35.744: I/DEBUG(11671):    "lat" : 46.1724292, 
10-31 14:55:35.744: I/DEBUG(11671):    "lng" : 16.0385437 
10-31 14:55:35.744: I/DEBUG(11671):    }, 
10-31 14:55:35.744: I/DEBUG(11671):    "southwest" : { 
10-31 14:55:35.744: I/DEBUG(11671):    "lat" : 45.7876853, 
10-31 14:55:35.744: I/DEBUG(11671):    "lng" : 15.8190462 
10-31 14:55:35.744: I/DEBUG(11671):    } 
10-31 14:55:35.744: I/DEBUG(11671):   }, 
... 
10-31 14:55:35.744: I/DEBUG(11671):      "html_instructions" : "Slight \u003cb\u003eright\u003c/b\u003e toward \u003cb\u003eVelika Petrovagorska\u003c/b\u003e", 
10-31 14:55:35.744: I/DEBUG(11671):      "maneuver" : "turn-slight-right", 
10-31 14:55:35.744: I/DEBUG(11671):      "polyline" : { 
10-31 14:55:35.744: I/DEBUG(11671):       "points" : "yxtxGixu`[email protected]?QFOJUL[[email protected][email protected]@@[email protected][email protected]_AI{@IiAEYEWCQCY?Y?]@[email protected]" 
10-31 14:55:35.744: I/DEBUG(11671):      }, 
10-31 14:55:35.744: I/DEBUG(11671):      "start_location" : { 
10-31 14:55:35.744: I/DEBUG(11671):       "lat" : 46.1506939, 
10-31 14:55:35.744: I/DEBUG(11671):       "lng" : 16.0091667 
10-31 14:55:35.744: I/DEBUG(11671):      }, 
10-31 14:55:35.744: I/DEBUG(11671):      "travel_mode" : "DRIVING" 
10-31 14:55:35.744: I/DEBUG(11671):     }, 
10-31 14:55:35.744: I/DEBUG(11671):     { 
10-31 14:55:35.744: I/DEBUG(11671):      "distance" : { 
10-31 14:55:35.744: I/DEBUG(11671):       "text" : 

我失去了這部分從響應:

 "overview_polyline" : { 
     "points" : "iptxGcxu`[email protected]@[email protected]@[email protected][email protected]}[email protected]}@[email protected]@[email protected]}[email protected]@}EUkC^[email protected]@[email protected]@uCWuFfAmEfB}[email protected]@[email protected]}[email protected]@`[email protected]@[email protected]@jNmAlDoBbFO`[email protected]|@xC|B|IjBdBjAbEjBxAxAW`BOvB|BvDhEpHlC|[email protected]@[email protected]@[email protected]@_AzAbApEhAlC`AiB`AoAvC^[email protected]}[email protected]~BfB][email protected][email protected]\\[email protected][email protected]\\`[email protected]@lGAzMtA`IlGdHzJtFlF`B|[email protected]@[email protected][email protected]|@hCQbF`[email protected][[email protected]@`[email protected]@tC?tCGvA^[email protected]|[email protected]{[email protected]@[email protected]}M~JuD~VwGtQ{[email protected][email protected]@nQbAnK[[email protected]`@[email protected][email protected][email protected]`@[email protected]@~In\\rFjP|GfMzHvJzIjH|[email protected]^|I`EzFxCrMhJjSnSbHhIvSvTtGxG|MzLjQlLzUzK`KfD`[email protected]@pNxT|[email protected]`@[email protected]|Q_B|[email protected]~[oD|WmHjWuL|[[email protected]@[email protected]{[email protected]{JlSaIdFwCvDcDbCwB|[email protected]@fAoABuAwAmAiCeJ}SyJwUaJiWuFwVwC}[email protected][email protected]@[email protected]}@`Bc_ApGkdB~DodAtA}[email protected]}[email protected]@y^[email protected]@[email protected][email protected][email protected]@U|@zCeB|[email protected]`@kOfL}Y`[email protected]{I{[email protected][email protected][email protected]@[email protected]|[email protected]~M{@[email protected]@}[email protected]" 
    }, 
    "summary" : "E59", 
    "warnings" : [], 
    "waypoint_order" : [] 
    } 
    ], 
"status" : "OK" 
} 

任何人有任何想法,爲什麼我可以不會得到全部答覆,然後只解析「overview_polyline」標籤?

+0

不知道如果你能相信什麼是logcat的diplayed,因爲那隻能說明數據的4096b和你的JSON響應是巨大的 – 2014-10-31 14:13:34

回答

1

您無法從jsonResponse對象正確讀取它。您嘗試從基本Json讀取,但它實際上是從「路由」標記中的一個Jsons中讀取的。你從響應採摘第一路徑代碼將是這樣的:

JsonArray routes = jsonResponse.optJsonArray("routes"); 
if (routes != null && route.length() > 0) { 
    JsonObject firstRoute = route.optJsonObject(1); 
    if (firstRoute != null) { 
     JsonArray waypoints = firstRoute.optJsonArray("waypoint_order"); 
     JsonObject overviewPolilyne = firstRoute.optJsonObject("overview_polyline"); 
    } 
} 
+0

我需要只有「polyline_overview」對象,但無論我嘗試多麼努力,我都無法得到它。我對JSON並不太滿意。你能編輯代碼來獲得只有polyline_overview對象嗎? 謝謝:) – 2014-11-05 17:51:21

+0

我更新了,應該工作。唯一的區別是讀取對象而不是數組。 – azertiti 2014-11-05 22:52:38

+0

非常感謝,芬妮讓它工作! :d – 2014-11-06 12:05:54