我正在尝试下载并显示折线。
我有这个异步任务设置:
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`BSMQGy@QWKWQOOMKKOGMCO?QFOJUL[FQ@OB_@Bs@@k@?e@E_AI{@IiAEYEWCQCY?Y?]@m@"
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`Bu@r@wAe@uCa@mDqBj@eD_@}JG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wVwC}SeBcUs@u_@Zwk@b@y}@`Bc_ApGkdB~DodAtA}_Aw@}b@c@y^b@al@Fmi@uA_t@_D_cBcByy@BwGt@U|@zCeB|Ck@`@kOfL}Y`QkPlHwBv@{I{h@sCwLkCiRoWmyA_Jey@eK_y@WqCaAPuGhBqVnFyDh@cBj@R|Ax@vFhB~M{@JkLh@oLp@}K~CuF~ByAh@NdA"
},
"summary" : "E59",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
任何人都知道为什么我无法得到完整的回复,然后只解析“overview_polyline”标签?
答案 0 :(得分:1)
您没有从jsonResponse对象中正确读取它。你试着从基础Json读取,但它实际上是在#34;路由&#34;中的一个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");
}
}