无法从json解析mapbox directionsroute类

a8jjtwal  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(386)

我在后端使用mapboxrestapi来创建路由。下面是一个简化的代码:

public class MapBoxRequest {

  // using string pattern just for convenience
  private static final String PATTERN =
      "https://api.mapbox.com/directions/v5/mapbox/walking/%s,%s;%s,%s?alternatives=true&geometries=geojson&steps=true&access_token=%s";

  public static void main(String[] args) throws IOException, URISyntaxException {
    HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();

    String accessToken =
        "access_toekn";

    // set right location values
    String string = String.format(PATTERN, longitude1, latitude1, longitude2, latitude2, accessToken);

      URI uri = new URI(string);

    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(uri.toString()));
    String rawResponse = request.execute().parseAsString();

    // HERE I AM GETTING EXCEPTION, THIS CODE IS SUPPOSED TO BE CALLED IN ANDROID APP
    DirectionsResponse.fromJson(rawResponse);
  }
}

使用这些maven依赖项:

<dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client</artifactId>
        <version>1.38.0</version>
 </dependency>

 <dependency>
       <groupId>com.mapbox.mapboxsdk</groupId>
       <artifactId>mapbox-sdk-services</artifactId>
       <version>5.6.0</version>
 </dependency>

但当我试图分析 DirectionsResponse 从字符串初始化我得到以下异常:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected 
a string but was BEGIN_OBJECT at line 1 column 546 path $.routes[0].legs[0].steps[0].geometry
    at com.google.gson.Gson.fromJson(Gson.java:939)
    at com.google.gson.Gson.fromJson(Gson.java:892)
    at com.google.gson.Gson.fromJson(Gson.java:841)
    at com.google.gson.Gson.fromJson(Gson.java:813)
    at com.mapbox.api.directions.v5.models.DirectionsResponse.fromJson(DirectionsResponse.java:133)
    at dating.walking.service.walking.setup.MapBoxRequest.main(MapBoxRequest.java:34)
Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 546 path $.routes[0].legs[0].steps[0].geometry
    at com.google.gson.stream.JsonReader.nextString(JsonReader.java:825)

上面的代码就是一个例子。实际上,android/ios客户机应该下载json,解析它,并在导航中使用它 MapBox Navigation SDK . 事情是在android应用程序中,我得到了和上面一样的异常。
我的问题是-如何在后端创建路由,将其作为json发送到客户端,解析json并启动导航?

8i9zcol2

8i9zcol21#

使用 MapBox Java SDK 而不是 MapBox REST API 和配置 Gson 库帮助我序列化和反序列化 ReponseRoute 班级。
下面是一个简化的代码:

public class MapBoxSdkSample {

  public static void main(String[] args) {
    String accessToken = "*****";

    Point originPoint = Point.fromLngLat(**,**);
    Point destinationPoint = Point.fromLngLat(**,**);

    MapboxDirections client =
        MapboxDirections.builder()
            .origin(originPoint)
            .destination(destinationPoint)
            .overview(DirectionsCriteria.OVERVIEW_FULL)
            .profile(DirectionsCriteria.PROFILE_WALKING)
            .accessToken(accessToken)
            .build();

    client.enqueueCall(
        new Callback<>() {
          @Override
          public void onResponse(
              Call<DirectionsResponse> call, Response<DirectionsResponse> response) {

            // boilerplate code
            if (response.body() == null) {
              System.out.println(
                  "No routes found, make sure you set the right user and access token.");
              return;
            } else if (response.body().routes().size() < 1) {
              System.out.println("No routes found");
              return;
            }

            DirectionsRoute directionsRoute = response.body().routes().get(0);

            Gson gson =
                new GsonBuilder()
                    .registerTypeAdapterFactory(DirectionsAdapterFactory.create())
                    .create();
            String s1 = gson.toJson(directionsRoute);

            // I wasn't getting exceptions here and the object was populated with data
            DirectionsRoute parsedFromJsonRoute = gson.fromJson(s1, DirectionsRoute.class);
          }

          @Override
          public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
            System.out.println("Error: " + throwable.getMessage());
          }
        });
  }
}

相关问题