here-apihere-android

Unable to use API call in HERE SDK (Android)


I am trying to use the HERE SDK Explorer Edition for Android and I need to make an API call to the HERE routes API. The API call itself works in the web browser, but I get "Routing error: 1, exception: Requested resource not found, error code: 404" when trying to use it in the Android application.

The API call is: "https://router.hereapi.com/v8/routes?apikey=-YOUR_API_KEY&origin=52.524465%2C13.382334&destination=52.525301%2C13.399844&return=polyline%2Csummary%2Cactions%2Cinstructions%2CrouteHandle&transportMode=car"

as shown in the documentation: "https://developer.here.com/documentation/android-sdk-explore/4.13.5.0/dev_guide/topics/routing.html#import-routes-from-other-services"

The code I have tried to use the API in the SDK is as follows:

 routingEngine.importRoute(
                        new RouteHandle(apiCall),
                        new RefreshRouteOptions(TransportMode.CAR),
                        new CalculateRouteCallback() {
                            @Override
                            public void onRouteCalculated(@Nullable RoutingError routingError, @Nullable List<Route> routes) {
                                if (routingError == null) {
                                    Route route = routes.get(0);
                                    showRouteDetails(route);
                                    showRouteOnMap(route);
                                    getManeuverInstructions(route);
                                    logRouteViolations(route);
                                } else {
                                    Log.e("Error while calculating a route:", routingError.toString());
                                }
                            }
                        }
                );

Solution

  • Take a look at the documentation:

    You need to replace YOUR_API_KEY with the actual key you have in use.

    Secondly, take a look at this code snippet:

    routingEngine.importRoute(new RouteHandle("routeHandleStringFromBackend"),
                          new RefreshRouteOptions(TransportMode.CAR),
                          new CalculateRouteCallback() {
            @Override
            public void onRouteCalculated(@Nullable RoutingError routingError, @Nullable List<Route> list) {
                if (routingError == null) {
                    Route newRoute = list.get(0);
                    // ...
                } else {
                    // Handle error.
                }
            }
        });
    

    The RouteHandle("routeHandleStringFromBackend") requires to set a string: Replace "routeHandleStringFromBackend" with the actual handle you have in use, for example: RouteHandle("FGFHJG454fFD_MYHANDLE")

    Note that all of this is only meant to import existing routes you have calculated via web. If you just want to calculate a route, please try to read this example:

    Waypoint startWaypoint = new Waypoint(startGeoCoordinates);
    Waypoint destinationWaypoint = new Waypoint(destinationGeoCoordinates);
    
    List<Waypoint> waypoints =
            new ArrayList<>(Arrays.asList(startWaypoint, destinationWaypoint));
    
    routingEngine.calculateRoute(
            waypoints,
            new CarOptions(),
            new CalculateRouteCallback() {
                @Override
                public void onRouteCalculated(@Nullable RoutingError routingError, @Nullable List<Route> routes) {
                    if (routingError == null) {
                        Route route = routes.get(0);
                        showRouteDetails(route);
                        showRouteOnMap(route);
                        logRouteViolations(route);
                    } else {
                        showDialog("Error while calculating a route:", routingError.toString());
                    }
                }
            });
    

    With this you do not need to initiate an "API call", you just use the HERE SDK to calculate a route - under the hood it will, however, contact a backend service.

    On the other hand, you can also calculate routes with the JavaScript API - via web. In order to use such routes with the HERE SDK you need to import them. For this you need to know the route handle which you can obtain from the JavaScript API.