我正在制作一个应该指导用户(导航(的安卓应用程序。我创建了一个地图活动,地图工作正常。我可以向 Google Directions API 发送请求并获取 JSON 响应。JSON 响应在单独的"数据解析器"类中解析。"parse"方法能够从每个步骤中读取"折线"对象,对其进行解码并绘制与地图上的折线点对应的线。我需要解析器也从每个步骤中检索字符串"机动"并返回(起始位置,机动(的哈希图。但是,当我尝试在备用"parse1"方法中执行此操作时,永远不会检索到每个步骤中与"机动"对应的字符串。下面是数据解析器代码:
package com.example.application.googlemapsdrawroute;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DataParser {
/** Receives a JSONObject and returns a list of lists containing latitude
and longitude */
public List<List<HashMap<String,String>>> parse(JSONObject jObject){
List<List<HashMap<String, String>>> routes = new ArrayList<>() ;
JSONArray jRoutes;
JSONArray jLegs;
JSONArray jSteps;
try {
jRoutes = jObject.getJSONArray("routes");
/** Traversing all routes */
for(int i=0;i<jRoutes.length();i++){
jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
List path = new ArrayList<>();
/** Traversing all legs */
for(int j=0;j<jLegs.length();j++){
//String navig = "";
jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
/** Traversing all steps */
for(int k=0;k<jSteps.length();k++){
String polyline = "";
polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
//navig= (String)((JSONObject)jSteps.get(k)).get("maneuver");
List<LatLng> list = decodePoly(polyline);
/** Traversing all points */
for(int l=0;l<list.size();l++){
HashMap<String, String> hm = new HashMap<>();
hm.put("lat", Double.toString((list.get(l)).latitude) );
hm.put("lng", Double.toString((list.get(l)).longitude) );
path.add(hm);
}
}
routes.add(path);
}
}
} catch (JSONException e) {
e.printStackTrace();
}catch (Exception e){
}
return routes;
}
public HashMap<LatLng,String> parse1(JSONObject jObject){
HashMap<LatLng, String> navig = new HashMap<>() ;
JSONArray jRoutes;
JSONArray jLegs;
JSONArray jSteps;
try {
jRoutes = jObject.getJSONArray("routes");
/** Traversing all routes */
for(int i=0;i<jRoutes.length();i++){
jLegs = ((JSONObject)jRoutes.get(i)).getJSONArray("legs");
List path = new ArrayList<>();
/** Traversing all legs */
for(int j=0;j<jLegs.length();j++){
String navig1 = "";
jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
/** Traversing all steps */
for(int k=0;k<jSteps.length();k++){
//String polyline = "";
LatLng startpt = new LatLng((Double)((JSONObject)((JSONObject)jSteps.get(k)).get("start_location")).get("lat"),(Double)((JSONObject)((JSONObject)jSteps.get(k)).get("start_location")).get("lng"));
navig1= ((JSONObject) jSteps.get(k)).getString("maneuver");
Log.d("Navig", navig1);
Log.d("LatLng", startpt.toString());
//List<LatLng> list = decodePoly(polyline);
/** Traversing all points
for(int l=0;l<list.size();l++){
HashMap<String, String> hm = new HashMap<>();
hm.put("lat", Double.toString((list.get(l)).latitude) );
hm.put("lng", Double.toString((list.get(l)).longitude) );
path.add(hm);
}*/
navig.put(startpt,navig1);
}
//routes.add(path);
}
}
} catch (JSONException e) {
e.printStackTrace();
}catch (Exception e){
}
return navig;
}
/**
* Method to decode polyline points
* Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
* */
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat / 1E5)),
(((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
}
当我运行这段代码时,logcat 中的响应是:
2432-4224/com.example.application.googlemapsdrawroute W/System.err: at com.example.application.googlemapsdrawroute.DataParser.parse1(DataParser.java:94)
DataParser 类中的第 94 行是:
navig1= ((JSONObject) jSteps.get(k)).getString("maneuver");
这有错吗?如果是这样,如何在每一步中从"操作"中检索字符串?
只需在第 94 行之前添加一个 if 语句作为
if((((JSONObject) jSteps.get(k))).get("maneuver")!=null)
它会起作用。