Mapbox Directions API返回状态代码为422的无法使用的响应

Kar*_*lSC 2 android directions mapbox

我正在使用MapBox方向与Android,我试图获取方向,以便稍后在地图上绘制.

但是,我从调用中得到的响应永远不能用作response.body()返回null.

当我调用response.code()时,我得到状态代码422,根据Mapbox API意味着"给定的请求无效.响应的消息密钥将保存无效输入的解释".

response.message()返回字符串"Unknown".

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

我使用了相同的代码,因为这个堆栈溢出应答应该可行,但事实并非如此.任何帮助,将不胜感激.

继承我的方法:

    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();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

我的进口:

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;
Run Code Online (Sandbox Code Playgroud)

我的朋友:

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

我尝试了不同国家/地区的不同职位,以及新的访问权限,并且启用了ass权限,但结果相同.我究竟做错了什么?

非常感谢.

小智 6

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

构造函数很重要.

Position pos = Position.fromCoordinates(/*LON FIRST*/lon, /*LAT SECOND*/ lat);
Run Code Online (Sandbox Code Playgroud)

例:

Position origin = Position.fromCoordinates(location.getLongitude(),location.getLatitude());
Run Code Online (Sandbox Code Playgroud)

希望这对您或未来的顾问有所帮助.