如何解析Google Directions API的API响应,以绘制从a点到B点的路线?我从我的web的中间件得到我的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":"gc}wAsjzaV]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":"ee}wAenzaVmBhAmAp@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":"eo}wAsgzaVU_@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~wAg}zaVOi@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>Dou00f1a Julia Vargas Ave</b><div style="font-size:0.9em">Destination will be on the right</div>",
"maneuver":"turn-left",
"polyline":{
"points":"io_xAyo{aVS@?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":"gc}wAsjzaVs@yAIWmBhAqEfCo@gASWkC{BuCmCoB{AcGeEo@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,所以我通过web的中间件访问它。我真的需要帮助,我已经被这个问题困扰了三天了。很抱歉问了太多问题。
如何解析Google Directions API的API响应,以绘制从a点到B点的路线?
API的响应是一系列的小行程。你可以把它想象成谷歌地图应用程序,每次你在每一步结束时都会给你一些反馈。除此之外,我认为API结构非常清楚,字段名称描述它们的含义也非常清楚。如果你需要我详细介绍一下,请告诉我。
我从我的web的中间件得到我的API响应。我需要为此创建一个POJO吗。
您肯定应该在这里使用POJO(换句话说,DTO(。数据定义清晰,结构清晰,java对象比在JsonNode中漫游更容易导航和使用。当用户使用导航时,您还必须处理许多不同的场景(更改路线、断开连接、应用程序进入睡眠状态(,因此您必须不断重新查询所述数据以更新地图。不使用POJO会给你带来很多麻烦和难闻的代码。
RoboPOJOgenerator是一个很好的从json制作POJO的工具吗?
就我个人而言,我喜欢使用http://www.jsonschema2pojo.org/生成我的域类,但你的建议也应该能够做同样的事情。