从带有GAE端点的float强制转换后的双精度值无效

2024-06-12 16:07:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用googlecloud的endpoints API并发送double,这是在Android中由float转换的。大多数情况下,这种方法都很有效,但我注意到,对于少量设备,我会遇到以下错误:

400错误请求

{
    "code": 400,
    "errors": [{
        "domain": "global",
        "location": "longitude",
        "locationType": "parameter",
        "message": "Invalid double value: '116.31765747070313'.",
        "reason": "invalidParameter"
    }],
    "message": "Invalid double value: '116.31765747070313'."
}

以下是android端的代码:

void callEndpoints(final int level, final float latitude, final float longitude) {
    try {
        API.GetPlacesFromLocationV2 remoteData = cbAPI.getPlacesFromLocationV2();

        remoteData.setLatitude((double)latitude);
        remoteData.setLongitude((double)longitude);
        remoteData.setLevel((long) level);

        Result res = remoteData.execute();

        //Do stuff with res
    } catch (IOException e) {
        //print error
    }
}

下面是python服务器端代码。你知道吗

PLACES_LOCATION_SEARCH_V2_CONTAINER = endpoints.ResourceContainer(
   message_types.VoidMessage,
   level=messages.IntegerField(1),
   longitude=messages.FloatField(2),
   latitude=messages.FloatField(3)
)

def getCellIdx(level, lon, lat):
   scale = pow(2, level)
   cellWidth = FULL_WIDTH / scale
   cellHeight = FULL_HEIGHT / scale

   wIdx = int((lon + 180.0) / cellWidth)
   hIdx = int((lat + 90.0) / cellHeight)

   return level * 100000000 + hIdx * 10000 + wIdx

@endpoints.method(PLACES_LOCATION_SEARCH_V2_CONTAINER, 
   DataCollection, 
   path="getPlacesFromLocation_v2", http_method='GET', 
   name="getPlacesFromLocation_v2")
def getPlacesFromLocation_v2(self, request):
  cellIdx = placesGridSearch.getCellIdx(request.level,request.longitude,request.latitude)
  #Do stuff with cellIdx, includes datastore access and memcache usage
  return DataCollection(cellIdx)

谢谢你的帮助。你知道吗

编辑: 一些附加信息,如果我使用相同的云经度值通过API浏览器运行它,我会得到相同的错误,但如果我在本地开发服务器上运行它,则不会得到相同的错误。 看起来endpoints函数从未被调用(没有日志),但是有一个/\u ah/spi/BackendService.logMessages文件描述完全相同错误的日志。你知道吗


Tags: apimessagerequest错误floatlevelfinalint