heremaps / here-android-sdk-examples

Java-based projects using the HERE SDK for Android.
Apache License 2.0
145 stars 191 forks source link

RoutingError.NETWORK_COMMUNICATION issue #308

Closed liridonsadiku closed 4 years ago

liridonsadiku commented 4 years ago

Hi there, I have a strange issue with some devices(samsung galaxy s8+, tablet tab a 10 inch) with the error network_communication while we try to create a route. I have tried with wifi and mobile data, but the same error is happenging. I have added: coreRouter.setConnectivity(CoreRouter.Connectivity.ONLINE); and I have updated the sdk to the latest version, but without results.

Here is my method when i call to create a route:

private void createRoute(GeoCoordinate startGeoCord, GeoCoordinate destinationGeoCord, ArrayList<Integer> pickupId, boolean isSwitchPickups, boolean isPostpone) {
    /* Initialize a CoreRouter */
    CoreRouter coreRouter = new CoreRouter();
    /* Initialize a RoutePlan */
    RoutePlan routePlan = new RoutePlan();
    /*
     * Initialize a RouteOption.HERE SDK allow users to define their own parameters for the
     * route calculation,including transport modes,route types and route restrictions etc.Please
     * refer to API doc for full list of APIs
     */
    RouteOptions routeOptions = new RouteOptions();
    /* Other transport modes are also available e.g Pedestrian */

    // routeOptions.setTransportMode(RouteOptions.TransportMode.TRUCK);
    //setVehicleType();

    //                Car = 0,

// Pedestrian = 1, // PublicTransport = 2, // Track attribute((deprecated)) = 3, // Truck = 5, // UrbanMobility = 6, // Bike = 7 // Scooter = 8

    if (m_fragment.preferences.getLastTruck().getVehicleType().getTransport_mode() == 0) {
        routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
    } else if (m_fragment.preferences.getLastTruck().getVehicleType().getTransport_mode() == 5) {
        routeOptions.setTransportMode(RouteOptions.TransportMode.TRUCK);
    } else if (m_fragment.preferences.getLastTruck().getVehicleType().getTransport_mode() == 7) { // bike not supported
        routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
    }

// // if (m_fragment.preferences.getLastTruck().getVehicleType().getTransport_mode() !=null) { // routeOptions.setTransportMode(RouteOptions.TransportMode.getMode(m_fragment.preferences.getLastTruck().getVehicleType().getTransport_mode())); // }

    if (m_fragment.preferences.getLengthOfTruck() != null && m_fragment.preferences.getLengthOfTruck().length() > 0) {
        routeOptions.setTruckLength(Float.parseFloat(m_fragment.preferences.getLengthOfTruck()));
    }

    if (m_fragment.preferences.getHeightOfTruck() != null && m_fragment.preferences.getHeightOfTruck().length() > 0) {
        routeOptions.setTruckHeight(Float.parseFloat(m_fragment.preferences.getHeightOfTruck()));
    }

    if (m_fragment.preferences.getWeightOfTruck() != null && m_fragment.preferences.getWeightOfTruck().length()>0){
        routeOptions.setTruckLimitedWeight(Float.parseFloat(m_fragment.preferences.getWeightOfTruck()));
    }

    if (m_fragment.preferences.getWidthOfTruck() != null && m_fragment.preferences.getWidthOfTruck().length()>0){
        routeOptions.setTruckWidth(Float.parseFloat(m_fragment.preferences.getWidthOfTruck()));
    }

    if (m_fragment.preferences.getAxlesOfTruck() != null && m_fragment.preferences.getAxlesOfTruck().length()>0){
        routeOptions.setTruckWeightPerAxle(Float.parseFloat(m_fragment.preferences.getAxlesOfTruck()));
    }

    System.out.println("routeOptionsss: weight: " + routeOptions.getTruckLimitedWeight() + " widht: " + routeOptions.getTruckWidth()  + " axle: " + routeOptions.getTruckWeightPerAxle());

    /* Calculate the shortest route available. */
    //routeOptions.setRouteType(RouteOptions.Type.FASTEST);//TRUCK MODE, FASTEST, Other routing types are not supported.

    routeOptions.setRouteType(getModeParameters());

    /* Calculate 1 route. */
    routeOptions.setRouteCount(1);
    /* Finally set the route option */

    routeOptions.setTollRoadsAllowed(!m_fragment.preferences.getIsToll());
    routeOptions.setHighwaysAllowed(!m_fragment.preferences.getIsHighway());
    routeOptions.setTunnelsAllowed(!m_fragment.preferences.getIsTunnels());
    routeOptions.setParksAllowed(!m_fragment.preferences.getIsParks());
    routeOptions.setDirtRoadsAllowed(!m_fragment.preferences.getIsDirtRoad());
    routeOptions.setCarpoolAllowed(!m_fragment.preferences.getIsCarpool());
    routeOptions.setFerriesAllowed(!m_fragment.preferences.getIsFerry());
    routeOptions.setCarShuttleTrainsAllowed(!m_fragment.preferences.getIsShuttleTrain());

    routePlan.setRouteOptions(routeOptions);

    /* Define waypoints for the route */
    routePlan.addWaypoint(new RouteWaypoint(startGeoCord));
    routePlan.addWaypoint(new RouteWaypoint(destinationGeoCord));
    /* Trigger the route calculation,results will be called back via the listener */

    EventBus.getDefault().post(new LogEvent("startGeoCord Latitude" + startGeoCord.getLatitude() + ", startGeoCord Longitude:" + startGeoCord.getLongitude()));
    EventBus.getDefault().post(new LogEvent("destinationGeoCord Latitude" + destinationGeoCord.getLatitude() + ", destinationGeoCord Longitude:" + destinationGeoCord.getLongitude()));

    coreRouter.setConnectivity(CoreRouter.Connectivity.ONLINE);
    System.out.println("coreRouter: " + coreRouter.getConnectivity().toString());

        coreRouter.calculateRoute(routePlan, new Router.Listener<List<RouteResult>, RoutingError>() {
            @Override
            public void onProgress(int i) {
                /* The calculation progress can be retrieved in this callback. */
            }

            @Override
            public void onCalculateRouteFinished(List<RouteResult> routeResults, RoutingError routingError) {
                /* Calculation is done.Let's handle the result */

                System.out.println("onCalculateRoute: 1");
                if (routingError == RoutingError.NONE) {
                    if (routeResults.get(0).getRoute() != null) {
                        m_route = routeResults.get(0).getRoute();
                        System.out.println("onCalculateRoute: 2");

                        nextRouteModels.clear();
                        maneuversArrayList.clear();
                        for (int i = 0; i < m_route.getManeuvers().size(); i++) {
                            NextRouteModel nr = new NextRouteModel(m_route.getManeuvers().get(i).getAction().name(),
                                    m_route.getManeuvers().get(i).getDistanceToNextManeuver(),
                                    m_route.getManeuvers().get(i).getRoadName(), m_route.getManeuvers().get(i).getIcon().ordinal());

                            nextRouteModels.add(nr);

                            //populate arraylist for maneuvers latitude and longitude
                            UpdateLocationBody updateLocationBody = new UpdateLocationBody(String.valueOf(m_route.getManeuvers().get(i).getCoordinate().getLatitude()),
                                    String.valueOf(m_route.getManeuvers().get(i).getCoordinate().getLongitude()));
                            maneuversArrayList.add(updateLocationBody);

                            for (int j = 0; j < maneuversArrayList.size(); j++) {
                                EventBus.getDefault().post(new LogEvent("latii " + maneuversArrayList.get(i).getLatitude() + "longii " + maneuversArrayList.get(i).getLongitude()));
                            }
                        }
                        /* Create a MapRoute so that it can be placed on the map */
                        if (mapRoute != null) {
                            m_map.removeMapObject(mapRoute);
                        }
                        mapRoute = new MapRoute(routeResults.get(0).getRoute());
                        /* Show the maneuver number on top of the route */
                        mapRoute.setManeuverNumberVisible(true);
                        mapRoute.setRenderType(MapRoute.RenderType.USER_DEFINED);
                        if (m_fragment.getActivity() != null) {
                            mapRoute.setTraveledColor(m_fragment.getActivity().getResources().getColor(R.color.dark_grey));
                            mapRoute.setColor(m_fragment.getActivity().getResources().getColor(R.color.colorGreen));
                        }
                        /* Add the MapRoute to the map */
                        m_map.addMapObject(mapRoute);
                        /*
                         * We may also want to make sure the map view is orientated properly
                         * so the entire route can be easily seen.
                         */
                        m_geoBoundingBox = routeResults.get(0).getRoute().getBoundingBox();
                        m_map.zoomTo(m_geoBoundingBox, Map.Animation.NONE, Map.MOVE_PRESERVE_ORIENTATION);

                        if (pickupId.size() > 0 && pickupId.get(0) != -7895) {

// if (isSwitchPickups) { // startAnotherPickupApi(pickupId + ""); // } else if (isPostpone) { // resumePickupApi(pickupId + ""); // } else { // startPickupApi(pickupId, destinationGeoCord.getLatitude() + "," + destinationGeoCord.getLongitude()); // }

                            if (!isSwitchPickups && !isPostpone) {
                                //  startPickupApi(pickupId, destinationGeoCord.getLatitude() + "," + destinationGeoCord.getLongitude());
                                isNavigationActive = true;
                                ParentActivity.isNavigationActive = true;
                            }

                        }
                        System.out.println("startNavigation is  called");
                        startNavigation();

                    } else {
                        System.out.println("onCalculateRoute: 3");

                        if (m_fragment.getActivity() != null)
                            Toast.makeText(m_fragment.getActivity(), "Error:route results returned is not valid " + routingError.toString(), Toast.LENGTH_LONG).show();
                        EventBus.getDefault().post(new HideNavigationEvent());
                        EventBus.getDefault().post(new LogEvent("createRoute1, start lat:" + startGeoCord.getLatitude() + " , start long:" + startGeoCord.getLongitude()));
                    }
                } else {
                    System.out.println("onCalculateRoute: 4");

                    if (m_fragment.getActivity() != null) {
                        System.out.println("onCalculateRoute: 5");

                        // EventBus.getDefault().post(new HideNavigationEvent());
                        EventBus.getDefault().post(new StartPickupEvent(-999, -999, null, null, false, null));// stop navigation with -999 when we cannot create a route
                        EventBus.getDefault().post(new LogEvent("IDsssssssssssssssssssss: " + m_fragment.jobOrder.getJobIds()));
                        if (m_fragment.jobOrder.getJobIds() != null) {
                            System.out.println("onCalculateRoute: 6");

                            HereMapsErrorWhileGeneratingRouteDialog dialog = new HereMapsErrorWhileGeneratingRouteDialog(m_fragment.getActivity(), routingError.toString(), m_fragment.jobOrder.getJobIds());
                            if (!dialog.isShowing()){
                                dialog.show();
                                dialog.setCanceledOnTouchOutside(false);
                                System.out.println("startJobORder called dialog IS Showing::::: ");
                            }

                        } else {
                            Toast.makeText(m_fragment.getActivity(), "Error:route calculation returned error code:" + routingError.toString(),
                                    Toast.LENGTH_LONG).show();
                        }

                        EventBus.getDefault().post(new LogEvent("createRoute2, start lat:" + startGeoCord.getLatitude() + " , start long:" + startGeoCord.getLongitude()));
                        EventBus.getDefault().post(new LogEvent("createRoute2, error" + routingError.name().toString()));
                        EventBus.getDefault().post(new LogEvent("createRoute2, error2" + routingError.toString()));
                    }

                }
            }
        });

}

This issue is happening only on some devices, in others is working well.

starand commented 4 years ago

@liridonsadiku thank you for reporting! Most probably the issue happens in okhttp library which has different behaviour on different devices. Do you see that error only in logs? Does routing works for you?

liridonsadiku commented 4 years ago

Thank you for your fast replaying, No it doesn't work! The issue is when we try to create a route from Start: [40.7604684, -73.9859151] to Destination: [40.70597400, -74.01261460] the callback onCalculateRouteFinished is fired and after that RoutingError returned NETWORK_COMMUNICATION error. Which means that here maps sdk cannot generate a route for these points with this type of error.

This is happening only in some devices(samsung galaxy s8+ and tablet tab a 10 inch)

Internet connection of device is stable so don't know the reason why this error is occurring.

for your information in my app I use retrofit: implementation 'com.squareup.retrofit2:retrofit:2.1.0'

starand commented 4 years ago

Thank you for the information. What is Android OS version on both devices?

liridonsadiku commented 4 years ago

Samsung Galaxy s8+ has OS -> Android 9 Tablet Tab A has OS -> 7.1.1

Version of SDK is: 3.13.3.105

Merlin1stHere commented 4 years ago

@liridonsadiku We still can't reproduce the issue. Maybe some kind of error appears in the log? Could you share logcat from your device? Could you share minimal sample that reproduces this issue?

starand commented 4 years ago

@liridonsadiku, can you try to reproduce using our github samples?

liridonsadiku commented 4 years ago

Hi there, I have noticed that now it is working in all devices including samsung galaxy s8+, I did not made any changes into my code. Maybe here maps team have made some changes on their own. Thanks.