如何在 android 中解析来自我的中间件的 Directions API 响应?
Posted
技术标签:
【中文标题】如何在 android 中解析来自我的中间件的 Directions API 响应?【英文标题】:How to parse Directions API Response from my middleware in android? 【发布时间】:2020-03-09 22:18:03 【问题描述】:如何解析 Google Directions API 的 API 响应以绘制从 A 点到 B 点的路线?我从我的网络中间件获得我的 API 响应。我需要为此创建一个 POJO 吗? RoboPOJOgenerator 是用于从 json 制作 POJO 的好工具吗?这个 json 是我的 okhttp 记录器的响应。
"data":[
"bounds":
"northeast":
"lat":14.5845178,
"lng":121.0601253
,
"southwest":
"lat":14.5722047,
"lng":121.0535383
,
"copyrights":"Map data \u00a92019",
"legs":[
"distance":
"text":"2.0 km",
"value":2013
,
"duration":
"text":"11 mins",
"value":638
,
"end_address":"J Vargas Bus Stop, Epifanio de los Santos, Ortigas Center, Mandaluyong, Metro Manila, Philippines",
"end_location":
"lat":14.5845178,
"lng":121.0569895
,
"start_address":"14/F Robinsons Cybergate Tower 1 Pioneer St. Metro Manila, Philippines",
"start_location":
"lat":14.5722047,
"lng":121.0540196
,
"steps":[
"distance":
"text":"70 m",
"value":70
,
"duration":
"text":"1 min",
"value":13
,
"end_location":
"lat":14.5725109,
"lng":121.0545851
,
"html_instructions":"Head <b>northeast</b> on <b>Pioneer St</b> toward <b>Reliance St</b><div style=\"font-size:0.9em\">Pass by HMR Trading Haus (on the left)</div>",
"polyline":
"points":"gcwAsjzaV]q@Ug@IW"
,
"start_location":
"lat":14.5722047,
"lng":121.0540196
,
"travel_mode":"DRIVING"
,
"distance":
"text":"0.2 km",
"value":210
,
"duration":
"text":"1 min",
"value":42
,
"end_location":
"lat":14.5741085,
"lng":121.0535383
,
"html_instructions":"Turn <b>left</b> onto <b>Reliance St</b>",
"maneuver":"turn-left",
"polyline":
"points":"eewAenzaVmBhAmAp@cCtA"
,
"start_location":
"lat":14.5725109,
"lng":121.0545851
,
"travel_mode":"DRIVING"
,
"distance":
"text":"0.6 km",
"value":573
,
"duration":
"text":"3 mins",
"value":159
,
"end_location":
"lat":14.5780025,
"lng":121.0569962
,
"html_instructions":"Turn <b>right</b> onto <b>Sheridan</b>",
"maneuver":"turn-right",
"polyline":
"points":"eowAsgzaVU_@Yg@GKKKECeCwB_ByAu@s@YWuAcAqCmB[UWQ@o@o@g@"
,
"start_location":
"lat":14.5741085,
"lng":121.0535383
,
"travel_mode":"DRIVING"
,
"distance":
"text":"0.8 km",
"value":829
,
"duration":
"text":"6 mins",
"value":336
,
"end_location":
"lat":14.5843678,
"lng":121.0599721
,
"html_instructions":"Continue onto <b>San Miguel Ave</b>",
"polyline":
"points":"og~wAgzaVOi@OMYWKGq@e@KIm@a@[SYSCAy@k@@m@WSe@_@WOeAw@]Uw@m@BgBKGQKMCMCM@S?_@BQ@G?y@BkAFw@?S?YBq@@S@AB"
,
"start_location":
"lat":14.5780025,
"lng":121.0569962
,
"travel_mode":"DRIVING"
,
"distance":
"text":"0.3 km",
"value":331
,
"duration":
"text":"1 min",
"value":88
,
"end_location":
"lat":14.5845178,
"lng":121.0569895
,
"html_instructions":"Turn <b>left</b> onto <b>Do\u00f1a Julia Vargas Ave</b><div style=\"font-size:0.9em\">Destination will be on the right</div>",
"maneuver":"turn-left",
"polyline":
"points":"io_xAyoaVS@?PAd@?b@?\\AxEA`A?RA\\AlB?Z"
,
"start_location":
"lat":14.5843678,
"lng":121.0599721
,
"travel_mode":"DRIVING"
],
"traffic_speed_entry":[
],
"via_waypoint":[
]
],
"overview_polyline":
"points":"gcwAsjzaVs@yAIWmBhAqEfCo@gASWkCBuCmCoBAcGeEo@g@Oi@i@e@wByAsBuAsCqByGcF]S[GsAFaABkAFw@?m@BeABqBDAv@C|ICzD"
,
"summary":"Sheridan and San Miguel Ave",
"warnings":[
],
"waypoint_order":[
]
],
"status":"SUCCESS"
这是我来自中间件的回应,我不能使用下面的代码
private String getRequestUrl(LatLng origin, LatLng dest)
String str_org = "origin=" + origin.latitude + "," + origin.longitude;
String str_dest = "destination=" + dest.latitude + "," + dest.longitude;
String sensor = "sensor=false";
String mode = "mode=driving";
String avoid = "avoid=tolls|highways";
String param = str_org + "&" + str_dest + "&" + sensor + "&" + mode + "&" + avoid;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + param + "&key=" + getString(R.string.API_KEY);
return url;
private String requestDirection(String reqUrl) throws IOException
String responseString = "";
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try
URL url = new URL(reqUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
//Get the response result
inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine()) != null)
stringBuffer.append(line);
responseString = stringBuffer.toString();
bufferedReader.close();
inputStreamReader.close();
catch (Exception e)
e.printStackTrace();
finally
if (inputStream != null)
inputStream.close();
httpURLConnection.disconnect();
return responseString;
因为我没有在我的移动应用程序中访问 Directions API,所以我通过我的网络中间件访问它。我真的需要帮助我已经被这个问题困扰了 3 天了。抱歉问了太多问题。
【问题讨论】:
【参考方案1】:如何解析 Google Directions API 的 API 响应以绘制从 A 点到 B 点的路线?
API 响应是一系列小行程。你可以把它想象成谷歌地图应用程序,每次你到达每一步结束时都会给你一些反馈。否则,我认为 API 结构非常清晰,字段名称描述它们的含义也非常清晰。如果您需要我更详细地了解它们,请告诉我。
我从我的网络中间件获得我的 API 响应。我需要为此创建一个 POJO。
您绝对应该在这里使用 POJO(换句话说,DTO)。数据被明确定义和结构化,Java 对象比在 JsonNode 周围游荡更容易导航和利用。当您的用户使用导航时,您还必须处理许多不同的情况(更改路线、失去连接、应用程序进入睡眠状态),因此您必须不断地重新查询所述数据以更新地图。不使用 POJO 会给你带来很多麻烦和臭代码。
RoboPOJOgenerator 是用来从 json 生成 POJO 的好工具吗?
我个人喜欢使用http://www.jsonschema2pojo.org/ 为我生成域类,但您的建议也应该能够做同样的事情。
【讨论】:
当我使用 RoboPOJO 生成器时,它会创建不同的类,例如腿、步数、边界。它们不在一个类文件中可以吗? 这是预期的结果。 “腿”是您在单一交通工具中执行的更大一部分旅程,即“步行”、“乘坐公共汽车”、“乘坐地铁”。然后它包含更小的块,“步骤”是详细的方向。您非常需要分离这些数据,因为它们必须转到应用程序上的不同 UI 元素,并且很可能必须由不同的业务逻辑处理。 如果你将所有东西都塞进一个类,你会发现自己传递了很多不必要的数据。此外,java json 解码器 GSON 或其他替代方案都无法在单个类中轻松扁平化如此复杂的 json 结构 感谢您的帮助我现在正在使用 roboPOJOgenerator,它工作正常,现在我可以访问 google 方向 API 的响应,我现在需要将这些数据绘制到我的 google 地图中。 为什么它说 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String delivery.blitz.client.Model.OverviewPolyline.getPoints()' on an null object reference 即使我的 json 响应中有点和概述折线?以上是关于如何在 android 中解析来自我的中间件的 Directions API 响应?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我没有收到来自我的相机意图的任何图片(在 Android 中)