使用移动后端启动器从数据存储区发送和返回数据

Asi*_*ono 3 android google-cloud-endpoints google-cloud-datastore

我正在尝试在我的Android应用程序中使用Mobile Backend Starter.为此,我需要在数据存储区中存储一些数据.我正在使用提供的对象,CloudEntity但我只能一致地插入和读取String.

这是我用来发送数据的示例代码:

CloudEntity entity = new CloudEntity(TEST_KIND_NAME);

entity.put(KEY_DATE, new Date(System.currentTimeMillis()));
entity.put(KEY_CALENDAR, Calendar.getInstance());
entity.put(KEY_LONG,  Long.valueOf(Long.MAX_VALUE));                
entity.put(KEY_INTEGER, Integer.valueOf(Integer.MAX_VALUE));
getCloudBackend().insert(entity, simpleHandler);
Run Code Online (Sandbox Code Playgroud)

这是我读回数据(下一个代码放在了onCompleteCloudBackendHandler:

StringBuffer strBuff = new StringBuffer();
strBuff.append("Inserted: \n");
strBuff.append("\tId = " + result.getId() + "\n");
Object o;
o = result.get(KEY_DATE);            
strBuff.append("\tDate was retrieved as : " + ((o == null)? "null" : o.getClass().getName()) + "\n");

o = result.get(KEY_CALENDAR);
strBuff.append("\tCalendar was retrieved as : " + ((o == null)? "null" : o.getClass().getName()) + "\n");

o = result.get(KEY_LONG);
strBuff.append("\tLong was retrieved as : " + ((o == null)? "null" : o.getClass().getName()) + "\n");

o = result.get(KEY_INTEGER);
strBuff.append("\tInteger was retrieved as : " + ((o == null)? "null" : o.getClass().getName()) + "\n");

o = result.get(KEY_BOOLEAN);
strBuff.append("\tBoolean was retrieved as : " + ((o == null)? "null" : o.getClass().getName()) + "\n");
mTvInfo.setText(strBuff);
Run Code Online (Sandbox Code Playgroud)

我得到的结果是:

数据插入DateCalendar返回null.

数据作为Integer退货插入BigDecimal.

插入的数据Long返回a String.

我的问题是:我可以发送(并回读)除字符串以外的其他数据吗?如果是这样.怎么样?

Asi*_*ono 5

经过一段时间的Android手机支持入门试验,我发现了一个链接到"一种"(非常有限的)文档:Mobile Backend Starter.

我发现如果您发送Integer(如果我信任de文档a Float或a Double),它将作为数字字段存储在DataStore中.并在您发送查询(通过)时返回.BigDecimalClouldQuery

但是,如果您将a Long作为属性传递给CloudEntity它,它将作为一个存储StringDataStore中并作为其返回.这不是微不足道的,因为String字段对允许的比较有限制.

如果您发送a DateTime,文档会告诉您将返回a String,但不要告诉您它也将存储在DataStoreString.

这很重要,因为你无法与Strings 进行所有比较.它只允许相等(和ne)比较(您不能String在查询中测试大于过滤属性的过滤器).因此,您无法将时间戳存储为Long(将转换为String您将无法比较它们),并且您无法设置时间戳,DateTime原因相同.而你无法存储Date或Calendar对象.

反正每次CloudEntity有两个DateTime调性质默认CloudEntity.PROP_CREATED_ATCloudEntity.PROP_UPDATED_AT.您可以使用此字段设置查询过滤器.为此,您需要将过滤器设置为

CloudQuery myQuery = new CloudQuery(<your kind name>);
myQuery.set<things>...
DateTime dateTime = new DateTime(<the time you want>);
myQuery.setFilter(F.gt(CloudEntity.PROP_UPDATED_AT, dateTime));
Run Code Online (Sandbox Code Playgroud)

需要使用a DateTime进行比较.如果你是勇敢的,你不能Date而不是DateTime用于这种比较.你会收到这个错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
    "code": 400,
    "errors": [
    {
       "domain": "global",
       "message": "java.lang.IllegalArgumentException: _updatedAt:java.util.LinkedHashMap is not a supported property type.",
       "reason": "badRequest"
    }
    ],
    "message": "java.lang.IllegalArgumentException: _updatedAt: java.util.LinkedHashMap is not a supported property type."
...
Run Code Online (Sandbox Code Playgroud)

其他奇怪的是,显然,你不能与之比较dateTime = new DateTime(0)(即1970-01-01T01:00:00.000 + 01:00),因为后端接收查询为:

query: (_kindName:"Data") AND ( _updatedAt > "1970-01-01T01:00:00.000+01:00" ), schema: {_kindName=STRING, _updatedAt=STRING}
Run Code Online (Sandbox Code Playgroud)

不会给出任何错误,但不会返回任何内容list: result: null.看起来它将比较视为一种String.如果您使用DateTime dateTime = new DateTime(1),您将发送一个查询:

list: executing query: {filterDto={operator=GT, values=[_updatedAt, 1970-01-01T01:00:00.001+01:00]},
Run Code Online (Sandbox Code Playgroud)

看起来和以前一样,但后端将执行查询:

 query: (_kindName:"Data") AND ( _updatedAt > 1 ), schema: {_kindName=STRING, _updatedAt=DOUBLE}
Run Code Online (Sandbox Code Playgroud)

正如我所看到的那样,DOUBLE我尝试提交一个Double而不是a DateTime,但它不起作用(没有错误但没有结果).

但是,好消息的每个人!我们可以用以下方式进行比较 Integer:

Integer anInteger = Integer.valueOf(0);
myQuery.setFilter(F.gt(CloudEntity.PROP_UPDATED_AT, anInteger));
Run Code Online (Sandbox Code Playgroud)

后端看到:

query: (_kindName:"Data") AND ( _updatedAt > 0 ), schema: {_kindName=STRING, _updatedAt=INT32}
Run Code Online (Sandbox Code Playgroud)

我们将获得预期值(自1970-01-01T01:00:00.000 + 01:00以来所有实体都已更新)

对不起我的错误(我的英语不好),我希望这可以帮助别人,至少可以节省他一些时间.