优步 Java SDK:rideEstimate、Vehicle 和 Driver 为空

Uber Java SDK : rideEstimate, Vehicle and Driver is null

我正在构建一个应用程序,我在沙盒环境中使用 Uber Java SDK。问题是 Uber 为 RideEstimate、Vehicle 和 Driver class.

返回的空值

Maven POM:

<dependency>
  <groupId>com.uber.sdk</groupId>
  <artifactId>rides</artifactId>
  <version>0.6.0</version>
</dependency>

我的示例 Servlet 代码:

    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (this.getOnceOnly()<2) {


        // Fetch the user's profile.
        UserProfile userProfile = uberRidesService.getUserProfile().execute().body();


        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().printf("Logged in as %s%n", userProfile.getEmail());

        // Publika West Gate 3.171282f 101.664771f
        // Publika East Side 3.170816f 101.667760f
        float startLatitude = 3.170730f;
        float startLongitude =  101.665129f;
        float destinationLatitude = 3.170816f;
        float destinationLongitude = 101.667760f;

        // Get products for location
        Response<ProductsResponse> responseGet = uberRidesService.getProducts(startLatitude, startLongitude).execute();
        List<Product> products = responseGet.body().getProducts();
        String productId = products.get(0).getProductId();
        System.out.println("responseGet: " + responseGet.code() + " " + responseGet.message());

        Response<TimeEstimatesResponse> responseTime = uberRidesService.getPickupTimeEstimate( startLatitude, startLongitude, productId).execute();
        List<TimeEstimate> timeEst = responseTime.body().getTimes();
        System.out.println("responseTime: " + responseTime.code() + " " + responseTime.message());
        //Response<PriceEstimatesResponse> responsePrice = uberRidesService.getPriceEstimates(3.170730f, 101.665129f, 3.171812f, 101.671201f).execute();
        Response<PriceEstimatesResponse> responsePrice = uberRidesService.getPriceEstimates(startLatitude, startLongitude, destinationLatitude , destinationLongitude).execute();
        List<PriceEstimate> priceEst = responsePrice.body().getPrices();
        System.out.println("responsePrice: " + responsePrice.code() + " " +  responsePrice.message());



        // Get upfront fare for product with start/end location
        RideRequestParameters rideRequestParameters = new RideRequestParameters.Builder().setPickupCoordinates(startLatitude, startLongitude)
               .setProductId(productId)
               .setDropoffCoordinates(destinationLatitude , destinationLongitude)
               .build();



        Response <RideEstimate> responseRideEstimate = uberRidesService.estimateRide(rideRequestParameters).execute();
        System.out.println("responseRideEstimate: " + responseRideEstimate.code() + " " + responseRideEstimate.message());
        RideEstimate rideEstimate = responseRideEstimate.body();
        //String estFareId = rideEstimate.getEstimate().getFareId();

        System.out.println("\nresponseRideEstimate.errorBody: "+ responseRideEstimate.errorBody());
        System.out.println("\nresponseRideEstimate.rawCode: "+ responseRideEstimate.raw().code());
        System.out.println("\nrideEstimate:"+ rideEstimate +"\n");
        System.out.println("\nrideEstimate.getPickupEstimate: "+ rideEstimate.getPickupEstimate());

        System.out.println("\nrideEstimate.getDisplay: "+ rideEstimate.getFare().getDisplay());
        System.out.println("\nrideEstimate.getFareId: "+ rideEstimate.getFare().getFareId());
        System.out.println("\nrideEstimate.getExpiresAt: "+ rideEstimate.getFare().getExpiresAt());
        System.out.println("\nrideEstimate.getValue: "+ rideEstimate.getFare().getValue());
        System.out.println("\nrideEstimate.getDistanceEstimate: "+ rideEstimate.getTrip().getDistanceEstimate());
        System.out.println("\nrideEstimate.getDistanceUnit: "+ rideEstimate.getTrip().getDistanceUnit());
        System.out.println("\nrideEstimate.getDurationEstimate: "+ rideEstimate.getTrip().getDurationEstimate());

        **// All these will cause an Http Error 500: null returned from Uber**
        //System.out.println("\ngetCurrencyCode: "+ rideEstimate.getEstimate().getCurrencyCode());
        //System.out.println("\ngetFareId: "+ rideEstimate.getEstimate().getFareId());
        //System.out.println("\ngetSurgeConfirmationHref: "+ rideEstimate.getEstimate().getSurgeConfirmationHref());
        //System.out.println("\ngetSurgeConfirmationId: "+ rideEstimate.getEstimate().getSurgeConfirmationId());
        //System.out.println("\ngetMinimum: "+ rideEstimate.getEstimate().getMinimum());
        //System.out.println("\ngetSurgeMultiplier: "+ rideEstimate.getEstimate().getSurgeMultiplier());
        //System.out.println("\ngetHighEstimate: "+ rideEstimate.getEstimate().getHighEstimate());
        //System.out.println("\ngetLowEstimate: "+ rideEstimate.getEstimate().getLowEstimate());
        //System.out.println("\ngetDisplay: "+ rideEstimate.getEstimate().getDisplay());

        String fareId = rideEstimate.getFare().getFareId();

        //double surgeRate = rideEstimate.getEstimate().getSurgeMultiplier();

        System.out.println("options: " +"\n");
        for (int i = 0; i< products.size(); i++ ) { 
            System.out.println(products.get(i).getProductId() +" tt " + products.get(i).getDisplayName() + " Plate " + ""     
                    + " other " + " " + rideEstimate.getFare().getDisplay() 
                    + "" );
            String output = "<br/> Pid "+ products.get(i).getProductId()
                    +" name "+products.get(i).getDisplayName()
                    +" cap "+ products.get(i).getCapacity();
                    //+" other "+ responseTime.message() ;
            response.getWriter().printf(output);
        }

        // Request ride with upfront fare for product with start/end location
        /*RideRequestParameters*/ rideRequestParameters = new RideRequestParameters.Builder().setPickupCoordinates(startLatitude, startLongitude)
               .setProductId(productId)
               .setFareId(fareId)
               .setDropoffCoordinates(destinationLatitude , destinationLongitude)
               .build();
        Response <Ride> responseRide = uberRidesService.requestRide(rideRequestParameters).execute();
        System.out.println("responseRide: " + responseRide.code() + " " + responseRide.message());
        //Ride ride = responseRide.body();
        Response<Void> responseUpdateRide = null;

        System.out.println("\nresponseRaw.code: "+ responseRide.raw().code());
        if (responseRide.code()==409) {
            System.out.println("\nresponseRide.errorBody: "+ responseRide.errorBody().string());
            System.out.println("\nresponseRide.errorBody: "+ responseRide.errorBody().contentType().toString());
            System.out.println("\nresponseRide.errorBody: "+ responseRide.headers().names());
            System.out.println("\nuberRidesService.getCurrentRide: "+ uberRidesService.getCurrentRide().toString());
            System.out.println("\nCancelCurrentRide: "+ uberRidesService.getCurrentRide().isCanceled());
            // Due to current user on trip, keep getting Error 409. 
            if (uberRidesService.getCurrentRide().isCanceled()!=true) {
                //uberRidesService.getCurrentRide().cancel();
                System.out.println("Canceling initiated...");
                //Response<Void> responseCancel = uberRidesService.cancelCurrentRide().execute();
                SandboxRideRequestParameters rideAcceptedParameters = new SandboxRideRequestParameters.Builder().setStatus(Ride.Status.DRIVER_CANCELED.toString()).build();
                /*Response<Void>*/ responseUpdateRide = uberRidesService.updateSandboxRide(responseRide.body().getRideId(), rideAcceptedParameters).execute();

                //System.out.println("\nresponseCancel: " + responseCancel.errorBody().toString());
                // Get products again
                System.out.println("Request product from Uber again");
                responseGet = uberRidesService.getProducts(startLatitude, startLongitude).execute();
                List<Product> products2 = responseGet.body().getProducts();
                String productId2 = products2.get(0).getProductId();

                rideRequestParameters = new RideRequestParameters.Builder().setPickupCoordinates(startLatitude, startLongitude)
                           .setProductId(productId2)
                           //.setFareId(fareId)
                           .setDropoffCoordinates(destinationLatitude , destinationLongitude)
                           .build();

                responseRideEstimate = uberRidesService.estimateRide(rideRequestParameters).execute();

                rideEstimate = responseRideEstimate.body();

                fareId = rideEstimate.getFare().getFareId();

                rideRequestParameters = new RideRequestParameters.Builder().setPickupCoordinates(startLatitude, startLongitude)
                           .setProductId(productId2)
                           .setFareId(fareId)
                           .setDropoffCoordinates(destinationLatitude , destinationLongitude)
                           .build();

                responseRide = uberRidesService.requestRide(rideRequestParameters).execute();
            }
            System.out.println("\nCancelCurrentRide: "+ uberRidesService.getCurrentRide().isCanceled());
        }
        //else {
        Ride ride = responseRide.body();
        System.out.println("\nride: "+ ride);
        System.out.println("\nride.getRideId: "+ ride.getRideId());
        System.out.println("\nride.getStatus(): "+ ride.getStatus());
        System.out.println("\nride.getProductId(): "+ ride.getProductId());
        System.out.println("\nride.getDestination(): "+ ride.getDestination().getLatitude() + ", " +ride.getDestination().getLongitude());
        System.out.println("\nride.getDestination().getEta(): "+ ride.getDestination().getEta()); // this is null
        System.out.println("\nride.getPickup(): "+ ride.getPickup().getLatitude() + ", " +ride.getPickup().getLongitude());
        System.out.println("\nride.getPickup().getEta(): "+ ride.getPickup().getEta()); // this is null
        System.out.println("\nride.getSurgeMultiplier(): "+ ride.getSurgeMultiplier());
        System.out.println("\nride.getVehicle(): "+ ride.getVehicle());
        **// All of the following returned HTTP Error 500: Problem accessing /. Reason: null pointer exception from Uber**
        //System.out.println("\nride.getDriver().getName(): "+ ride.getDriver().getName());
        //System.out.println("\nride.getLocation(): "+ ride.getLocation());
        //System.out.println("\nride.getVehicle(): "+ ride.getVehicle().toString());
        //System.out.println("\nride.getDriver().getName(): "+ ride.getDriver().toString());

        //}

        //System.out.println("\ngetRideId: "+ ride.getRideId());
        //System.out.println("\ngetProductId: "+ ride.getProductId());
        //String rideId = ride.getRideId();

        SandboxRideRequestParameters rideAcceptedParameters = new SandboxRideRequestParameters.Builder().setStatus(Ride.Status.ACCEPTED.toString()).build();
        responseUpdateRide = uberRidesService.updateSandboxRide(responseRide.body().getRideId(), rideAcceptedParameters).execute();

        // Request ride details from request_id
        //responseRide = uberRidesService.getRideDetails(rideId).execute();
        System.out.println("responseRide: " + responseRide.code() + " " + responseRide.message());

        System.out.println("\nride.getStatus(): "+ ride.getStatus());
        System.out.println("\nresponseUpdateRide: " + responseUpdateRide.code() + " " + responseUpdateRide.message());
        if (responseUpdateRide.code()!=202) {
            System.out.println("\nresponseUpdateRide.errorBody: " + responseUpdateRide.errorBody().string());
        }

从上面可以看出,RideEstimate、Vehicle 和 Driver class 都会导致空指针。真的很奇怪,我可以在没有 Vehicle 或 Driver 附加到乘车请求的情况下成功请求乘车。我找到了一个解决方法来传递作为 null 返回的 RideEstimate(请参阅上面的代码)但是我找不到 Vehicle 和 Driver 也为 null 的解决方法。

因此,如果有任何其他方法可以获取 Vehicle 且 Driver 为 null,请通知我。另外,最好有一些使用沙盒更新状态的示例。

当您请求乘车时 - 它以 "processing" 状态创建 - 此时,driver 仍然未知 - 因为 driver 未接受此请求然而。这就是为什么没有得到任何 driver 或车辆信息的原因。沙盒不会自动在行程中移动,但您可以手动将行程请求放入每个状态:

"Currently, the sandbox does not change states automatically the way a real request in production would. This endpoint gives the ability to walk an application through the different states of a ride request."

更多详情请参阅沙盒指南:https://developer.uber.com/docs/riders/guides/sandbox

要查看乘车请求的生命周期,请阅读此documentation