如何从我的android中间件解析Directions API响应?

问题描述 投票:0回答:1

我如何解析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":"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>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_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,所以我通过网络的中间件对其进行访问。我真的需要帮助,但是这个问题我已经困扰了3天了。抱歉问了太多问题。

java android json google-maps google-direction
1个回答
0
投票

我如何解析Google Directions API的API响应以绘制从我的A点到B点的路线?

api响应是一系列小旅行。您可以想象得到,当您到达每个步骤的末尾时,Google Maps api会告诉您以语音和其他方式执行某种操作。

我从Web的中间件获得API响应。我是否需要为此创建一个POJO

您肯定应该在这里使用POJO(换句话说,就是DTO)。数据已明确定义和结构化,并且与在JsonNode上四处游荡相比,java对象更易于导航和利用。

我个人喜欢使用http://www.jsonschema2pojo.org/来生成域类,但是您的建议也应该能够执行相同的操作。

© www.soinside.com 2019 - 2024. All rights reserved.