我正在构建一个应用程序,并在沙盒环境中使用uberjavasdk。问题是uber为rideestimate、vehicle和driver类返回的null。
Mavenpom:
<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());
}
从上面可以看出,rideeestimate、vehicle和driver类都会导致空指针。这真的很奇怪,我可以要求乘坐成功没有车辆或司机附加到乘坐请求。我找到了一个解决办法,让通过rideeestimate返回一个空值(见我的代码上面),但我找不到一个解决办法的车辆和司机也为空。
因此,如果有任何其他方法可以使车辆和驾驶员为空,请通知我。另外,有一些关于使用沙盒更新状态的例子也不错。
1条答案
按热度按时间mm9b1k5b1#
当你请求一次乘坐-它被创建为“处理”状态-在这一点上,司机仍然是未知的-因为司机还没有接受这个请求。这就是为什么我们不能得到任何司机或车辆信息的原因。沙盒不会自动在骑乘中移动,但您可以手动将骑乘请求置于每个状态:
“目前,沙盒不会像生产中的实际请求那样自动更改状态。此端点使应用程序能够遍历乘坐请求的不同状态。”
有关更多详细信息,请参阅沙盒指南:https://developer.uber.com/docs/riders/guides/sandbox.
要检查乘坐请求的生命周期,请阅读此文档。