Problem picking up a GSON variable

Asked

Viewed 62 times

-2

I’m making an app that uses the google maps Directions api, which returns me a json, for example:

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJeRLP8gx3GZURImtwaSWGJgY",
         "types" : [ "street_address" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJd515ygl3GZURnfYfYs3XfW4",
         "types" : [ "street_address" ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : -30.0096913,
               "lng" : -51.1507108
            },
            "southwest" : {
               "lat" : -30.0126831,
               "lng" : -51.15656300000001
            }
         },
         "copyrights" : "Dados cartográficos ©2017 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "0,8 km",
                  "value" : 847
               },
               "duration" : {
                  "text" : "10 minutos",
                  "value" : 624
               },
               "end_address" : "Tv. Prof. Emetério García Fernandes, 193 - Cristo Redentor, Porto Alegre - RS, 91370-380, Brasil",
               "end_location" : {
                  "lat" : -30.0126831,
                  "lng" : -51.1507108
               },
               "start_address" : "Av. Bernardi, 61 - Cristo Redentor, Porto Alegre - RS, 91040-030, Brasil",
               "start_location" : {
                  "lat" : -30.0096913,
                  "lng" : -51.15656300000001
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "0,8 km",
                        "value" : 847
                     },
                     "duration" : {
                        "text" : "10 minutos",
                        "value" : 624
                     },
                     "end_location" : {
                        "lat" : -30.0126831,
                        "lng" : -51.1507108
                     },
                     "html_instructions" : "Ande para Tv. Prof. Emetério García Fernandes, 193 - Cristo Redentor, Porto Alegre - RS, 91370-380, Brasil",
                     "polyline" : {
                        "points" : "pgdvDnofwHR?jBGEcE@cCBiC?[@yA@k@XO~AWt@Kp@Eh@C`AGfBK@sA@sAHAF?DyG"
                     },
                     "start_location" : {
                        "lat" : -30.0096913,
                        "lng" : -51.15656300000001
                     },
                     "steps" : [
                        {
                           "distance" : {
                              "text" : "71 m",
                              "value" : 71
                           },
                           "duration" : {
                              "text" : "1 min",
                              "value" : 43
                           },
                           "end_location" : {
                              "lat" : -30.0103252,
                              "lng" : -51.1565215
                           },
                           "html_instructions" : "Siga na direção \u003cb\u003esul\u003c/b\u003e na \u003cb\u003eAv. Bernardi\u003c/b\u003e em direção a \u003cb\u003eAv. Assis Brasil\u003c/b\u003e",
                           "polyline" : {
                              "points" : "pgdvDnofwHR?jBG"
                           },
                           "start_location" : {
                              "lat" : -30.0096913,
                              "lng" : -51.15656300000001
                           },
                           "travel_mode" : "WALKING"
                        },
                        {
                           "distance" : {
                              "text" : "0,3 km",
                              "value" : 303
                           },
                           "duration" : {
                              "text" : "3 minutos",
                              "value" : 205
                           },
                           "end_location" : {
                              "lat" : -30.0103521,
                              "lng" : -51.1533792
                           },
                           "html_instructions" : "Vire à \u003cb\u003eesquerda\u003c/b\u003e na \u003cb\u003eAv. Assis Brasil\u003c/b\u003e",
                           "maneuver" : "turn-left",
                           "polyline" : {
                              "points" : "pkdvDfofwHEcE@cCBiC?[@yA@k@"
                           },
                           "start_location" : {
                              "lat" : -30.0103252,
                              "lng" : -51.1565215
                           },
                           "travel_mode" : "WALKING"
                        },
                        {
                           "distance" : {
                              "text" : "0,2 km",
                              "value" : 247
                           },
                           "duration" : {
                              "text" : "3 minutos",
                              "value" : 200
                           },
                           "end_location" : {
                              "lat" : -30.0125358,
                              "lng" : -51.1529738
                           },
                           "html_instructions" : "Vire à \u003cb\u003edireita\u003c/b\u003e na \u003cb\u003eAv. do Forte\u003c/b\u003e",
                           "maneuver" : "turn-right",
                           "polyline" : {
                              "points" : "tkdvDr{ewHXO~AWt@Kp@Eh@C`AGfBK"
                           },
                           "start_location" : {
                              "lat" : -30.0103521,
                              "lng" : -51.1533792
                           },
                           "travel_mode" : "WALKING"
                        },
                        {
                           "distance" : {
                              "text" : "81 m",
                              "value" : 81
                           },
                           "duration" : {
                              "text" : "1 min",
                              "value" : 64
                           },
                           "end_location" : {
                              "lat" : -30.012562,
                              "lng" : -51.1521285
                           },
                           "html_instructions" : "Vire à \u003cb\u003eesquerda\u003c/b\u003e na \u003cb\u003ePassagem Dois\u003c/b\u003e",
                           "maneuver" : "turn-left",
                           "polyline" : {
                              "points" : "jydvD`yewH@sA@sA"
                           },
                           "start_location" : {
                              "lat" : -30.0125358,
                              "lng" : -51.1529738
                           },
                           "travel_mode" : "WALKING"
                        },
                        {
                           "distance" : {
                              "text" : "10 m",
                              "value" : 10
                           },
                           "duration" : {
                              "text" : "1 min",
                              "value" : 8
                           },
                           "end_location" : {
                              "lat" : -30.01265129999999,
                              "lng" : -51.1521164
                           },
                           "html_instructions" : "Vire à \u003cb\u003edireita\u003c/b\u003e na \u003cb\u003eR. Zéca Neto\u003c/b\u003e",
                           "maneuver" : "turn-right",
                           "polyline" : {
                              "points" : "nydvDxsewHHAF?"
                           },
                           "start_location" : {
                              "lat" : -30.012562,
                              "lng" : -51.1521285
                           },
                           "travel_mode" : "WALKING"
                        },
                        {
                           "distance" : {
                              "text" : "0,1 km",
                              "value" : 135
                           },
                           "duration" : {
                              "text" : "2 minutos",
                              "value" : 104
                           },
                           "end_location" : {
                              "lat" : -30.0126831,
                              "lng" : -51.1507108
                           },
                           "html_instructions" : "Vire à \u003cb\u003eesquerda\u003c/b\u003e na \u003cb\u003eTv. Prof. Emetério García Fernandes\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eO destino estará à direita\u003c/div\u003e",
                           "maneuver" : "turn-left",
                           "polyline" : {
                              "points" : "`zdvDvsewHDyG"
                           },
                           "start_location" : {
                              "lat" : -30.01265129999999,
                              "lng" : -51.1521164
                           },
                           "travel_mode" : "WALKING"
                        }
                     ],
                     "travel_mode" : "WALKING"
                  }
               ],
               "traffic_speed_entry" : [],
               "via_waypoint" : []
            }
         ],
         "overview_polyline" : {
            "points" : "pgdvDnofwH~BGEcE@cCBeDBeCXO~AWfBQjBKfBK@sA@sAHAF?DyG"
         },
         "summary" : "Av. Assis Brasil e Av. do Forte",
         "warnings" : [
            "Versão beta da rota a pé. Seja cuidadoso – Este trajeto pode não ter calçadas ou caminhos de pedestres."
         ],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}

I use gson to read this json and use in my application, one of the uses is this:

public void lerGSON(TransformJSONToGSONGoogleMapsDirections transform) {
    if(transform.getStatus().equalsIgnoreCase("OK")) {
        distance = transform.getRoutes().get(0).getLegs().get(0).getDistance().getText();
        duration = transform.getRoutes().get(0).getLegs().get(0).getDuration().getText();
        address = transform.getRoutes().get(0).getLegs().get(0).getEndAddress();

        Log.d(TAG, "address: " + address); //Retorna null

        ArrayList<Steps> steps = transform.getRoutes().get(0).getLegs().get(0).getSteps();

        Log.d(TAG, "travel mode: " + steps.get(0).getTravelMode()); //Retorna null

        latLng = new ArrayList<>();
        for(int i = 0; i < steps.size(); i++) {
            if(steps.get(i).getTravelMode().equalsIgnoreCase("WALKING")) {
                String polyline = steps.get(i).getPolyline().getPoints();

                for(LatLng p : decodePolyline(polyline)) {
                    latLng.add(p);
                }
            }
        }
        drawRoute(latLng, 255, 63, 81, 181);

        setResults();
    } else if(transform.getStatus().equalsIgnoreCase("ZERO_RESULTS")) {
        criarMsgErro("Não foi possível traçar uma rota para esse destino!");
    } else if(transform.getStatus().equalsIgnoreCase("OVER_QUERY_LIMIT")) {
        criarMsgErro("Um erro interno ocorreu, por favor tente novamente!");
    }
}

I’m not getting the travel_mode and the end_address json, I’ve checked the path several times and I think you’re right

cannot place gson classes because they are many (more than 20), but it is in the same json structure

and the value of travel_mode is not null in json

TransformJSONToGSONGoogleMapsDirections is the first class of gson, the first json object transform, therefore:

transform.getRoutes().get(0).getLegs().get(0).getEndAddress();

takes the end_address object of the Legs at position 0, of the Routes at position 0

And:

transform.getRoutes().get(0).getLegs().get(0).getSteps().get(0).getTravelMode();

takes the travel_mode object of Steps at position 0, Legs at position 0, Routes at position 0

1 answer

0


The problem was that json returned travel_mode, and in the classes I created used travelMode, thought gson identified these small differences.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.