gpt4 book ai didi

android - Mapbox Directions API 返回状态代码为 422 的不可用响应

转载 作者:太空狗 更新时间:2023-10-29 16:00:14 24 4
gpt4 key购买 nike

我正在将 MapBox Directions 与 android 一起使用,我正在尝试获取稍后在 map 上绘制的方向。

但是,我从调用中获得的响应永远无法使用,因为 response.body() 返回 null。

当我调用 response.code() 时,我得到状态代码 422,根据 Mapbox API 的意思是“给定的请求无效。响应的消息键将包含一个解释的无效输入。”

response.message() 返回字符串“Unknown”。

response.errorBody() 返回字符串“okhttp3.ResponseBody$1@38fb6f04”

我使用了与 this stack overflow answer 相同的代码这应该工作,但它没有。任何帮助,将不胜感激。

这是我的方法:

    private void getRoute(Position origin, Position destination) throws ServicesException {

MapboxDirections client = new MapboxDirections.Builder()
.setOrigin(origin)
.setDestination(destination)
.setProfile(DirectionsCriteria.PROFILE_CYCLING)
.setAccessToken("Same access token as the map uses")
.build();

client.enqueueCall(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
// You can get the generic HTTP info about the response
Log.d(TAG, "Response code: " + response.code());
if (response.body() == null) {
Log.e(TAG, "No routes found, make sure you set the right user and access token.");
return;
}

// Print some info about the route
currentRoute = response.body().getRoutes().get(0);
Log.d(TAG, "Distance: " + currentRoute.getDistance());
Toast.makeText(MainActivity.this, "Route is " + currentRoute.getDistance() + " meters long.", Toast.LENGTH_SHORT).show();

// Draw the route on the map
drawRoute(currentRoute);
}

@Override
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
Log.e(TAG, "Error: " + t.getMessage());
Toast.makeText(MainActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}

还有我的进口:

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import com.mapbox.services.directions.v5.DirectionsCriteria;
import com.mapbox.services.directions.v5.MapboxDirections;
import com.mapbox.services.directions.v5.models.DirectionsResponse;
import com.mapbox.services.directions.v5.models.DirectionsRoute;

还有我的gradle:

    compile ('com.mapbox.mapboxsdk:mapbox-android-services:1.0.0@aar'){
transitive=true
}

我在不同的国家尝试过不同的职位,以及一个打开了 ass 权限的新访问 token ,结果相同。我究竟做错了什么?

非常感谢。

最佳答案

检查您在构建 Position 对象时是否以正确的方式设置了纬度和经度。

构造函数很重要。

Position pos = Position.fromCoordinates(/*LON FIRST*/lon, /*LAT SECOND*/ lat);

示例:

Position origin = Position.fromCoordinates(location.getLongitude(),location.getLatitude());

希望这对您或 future 的顾问有所帮助。

关于android - Mapbox Directions API 返回状态代码为 422 的不可用响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37440281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com