0

我正在尝试从 Long 类型甚至 Integer 获取 Value 类类型(来自 google.datastore.v1),但似乎无法这样做。

这是似乎不情愿的代码部分

import com.google.datastore.v1.Value;

public class EntityHelper {
  public static Value makeValue(final Long value) {
    Value.Builder builder = Value.newBuilder();
    builder.setIntegerValue(value);
    return builder.build();
  }
}

运行时,此代码生成以下错误:

com.google.api.server.spi.SystemService invokeServiceMethod: exception occurred while calling backend method (SystemService.java:375)
java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    com/google/datastore/v1/Value$Builder.setGeoPointValue(Lcom/google/type/LatLng$Builder;)Lcom/google/datastore/v1/Value$Builder; @30: invokevirtual
  Reason:
    Type 'com/google/type/LatLng' (current frame, stack[1]) is not assignable to 'com/google/protobuf/GeneratedMessage'
  Current Frame:
    bci: @30
    flags: { }
    locals: { 'com/google/datastore/v1/Value$Builder', 'com/google/type/LatLng$Builder' }
    stack: { 'com/google/protobuf/SingleFieldBuilder', 'com/google/type/LatLng' }
  Bytecode:
    0x0000000: 2ab4 001a c700 122a 2bb6 007c b500 102a
    0x0000010: b600 37a7 000f 2ab4 001a 2bb6 007c b600
    0x0000020: 5a57 2a10 08b5 0009 2ab0               
  Stackmap Table:
    same_frame(@22)
    same_frame(@34)

    at com.google.datastore.v1.Value.toBuilder(Value.java:749)
    at com.google.datastore.v1.Value.newBuilder(Value.java:743)
    at com.app.data.impl.objectify.EntityHelper.makeValue(EntityHelper.java:931)
    at com.app.data.impl.objectify.EntityHelper.makeValue(EntityHelper.java:903)
    at com.app.apps.client.data.impl.BookingDao.getContactBookings(BookingDao.java:123)
    at com.app.apps.client.reporting.Endpoints.getContactListBookingsDataResponse(Endpoints.java:230)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:351)
    at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:119)
    at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:102)
    at com.google.api.server.spi.dispatcher.PathDispatcher.dispatch(PathDispatcher.java:50)
    at com.google.api.server.spi.EndpointsServlet.service(EndpointsServlet.java:72)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:848)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1772)
    at com.app.apps.client.servlet.InitializerFilter.doFilter(InitializerFilter.java:27)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at net.lightoze.gwt.i18n.server.I18nFilter.doFilter(I18nFilter.java:65)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.googlecode.objectify.ObjectifyFilter.doFilter(ObjectifyFilter.java:48)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at org.slim3.datastore.DatastoreFilter.doFilter(DatastoreFilter.java:55)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:582)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at com.google.apphosting.runtime.jetty9.ParseBlobUploadHandler.handle(ParseBlobUploadHandler.java:119)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1182)
    at com.google.apphosting.runtime.jetty9.AppEngineWebAppContext.doHandle(AppEngineWebAppContext.java:187)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:293)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at org.eclipse.jetty.server.Server.handle(Server.java:539)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:333)
    at com.google.apphosting.runtime.jetty9.RpcConnection.handle(RpcConnection.java:213)
    at com.google.apphosting.runtime.jetty9.RpcConnector.serviceRequest(RpcConnector.java:81)
    at com.google.apphosting.runtime.jetty9.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:134)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchServletRequest(JavaRuntime.java:757)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchRequest(JavaRuntime.java:720)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:690)
    at com.google.apphosting.runtime.JavaRuntime$NullSandboxRequestRunnable.run(JavaRuntime.java:882)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:270)
    at java.lang.Thread.run(Thread.java:748)

我似乎无法弄清楚为什么会产生这个错误以及我必须做些什么来修复它,欢迎任何帮助

4

1 回答 1

0

这里有一些想法:

import com.google.datastore.v1.Value;

public class EntityHelper {
  public static Value makeValue(final Long value) {
     Value.Builder builder = Value.newBuilder();
     builder.setIntegerValue(value);
     return builder.build();
  }
}

首先,您是否将 long 设置为整数。根据定义,这会导致数据丢失,因为 long 是 8 个字节,而 integer 是 4 个字节。

您可以尝试将类型设置为与您尝试使用的兼容的类型。

通常,纬度和经度表示为双精度值。希望这可以帮助您排除故障。

我不确定您使用的是哪个 LatLng 对象,但这是一个示例

[ https://developer.android.com/reference/androidx/car/cluster/navigation/LatLng]

作为一个例子,你可以试试这个:

import com.google.datastore.v1.Value;

public class EntityHelper {
  public static Value makeValue(final Double value) {
     Value.Builder builder = Value.newBuilder();
     builder.setDoubleValue(value);
     return builder.build();
  }
}

我不确定您的实现,但是另一种选择是使用 LatLng.Builder getGeoPointValueBuilder()

在此示例中,您将实现如下所示的内容:

...将需要不同的进口...

import com.google.datastore.v1.Value;

public class EntityHelper {
  public static LatLng makeLatLng(final LatLng value) {
     LatLng.Builder builder = LatLng.Builder.newBuilder(); 
     builder.setLatLng(value);
     return builder.build();
  }
}

正如我所说,我不确定您使用的是哪个 API,但可能会在这里四处寻找以定制您的解决方案:

[ http://googleapis.github.io/googleapis/java/all/latest/apidocs/com/google/type/LatLng.html]

于 2020-04-01T09:21:06.370 回答