发送和从数据存储retreaving数据与移动后端启动 [英] Sending and retreaving data from datastore with mobile backend starter

查看:267
本文介绍了发送和从数据存储retreaving数据与移动后端启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用手机后台启动在我的Andr​​oid应用程序。为了做到这一点,我需要一些数据存储中的数据存储。 我使用的是提供的对象 CloudEntity 但我只能一直插入和读字符串

I'm trying to use Mobile Backend Starter in my Android application. In order to do that I need to store some data in the Datastore. I'm using the provided object CloudEntity but I can only consistently insert and read String.

这就是例子code我用来发送数据:

That's the example code I used to send data:

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);

和我这是怎么读的数据备份(下一个code位于的onComplete CloudBackendHandler

and this is how I read the data back (next code goes in the onComplete in the CloudBackendHandler:

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);

和我所得到的结果是:

插入为日期数据和日历返回

插入为整数返回的BigDecimal

插入为数据返回字符串

我的问题是:我可以发送(和回读)不是'字符串的其他数据?如果是的话。怎么样?

My question is: Can I send (and read back) other data than `String? And if so. How?

推荐答案

在使用Android手机已备份入门一段时间的实验,我发现一个链接到一个之类的(非常有限)的文件:的Mobile后端入门

After some time experimenting with the Android Mobile Backed Starter I found a link to a "sort of" (very limited) documentation: Mobile Backend Starter.

我发现的是,如果你发送一个整数(如果我相信德文档中的浮动双击)被存储在数据存储作为一个的数字的领域。并返回一个的BigDecimal 当你发送一个查询(通过 ClouldQuery )。

What I found is that if you send an Integer (and if I trust de documentation a Float or a Double) it is stored in the DataStore as a numeric field. And is returned as a BigDecimal when you send a query (through ClouldQuery).

不过,如果你传递一个在你的 CloudEntity ,它会被存储为一个属性字符串数据存储,然后返回等。这是不平凡的,因为字符串字段对允许的比较局限。

Nevertheless, if you pass a Long as a property in your CloudEntity, it will be stored as a String in the DataStore and returned as such. And this is not trivial, as String fields has limitations on allowed comparisons.

如果你发送一个<一个href="https://$c$c.google.com/p/google-api-java-client/source/browse/google-api-client/src/com/google/api/client/util/DateTime.java?r=ef7eabc5cb37e2a2199b4040ec92ba4d18cb9553"><$c$c>DateTime,该文件告诉你,你会得到一个字符串,但不告诉你,它会被存储在数据存储作为一个字符串了。

If you send a DateTime, the documentation tells you that you will get back a String, but don't tells you that it will be stored in the DataStore as a String too.

这是重要的,因为你不能做所有与字符串 S中的比较。它只允许平等(和NE),比较(你不能比过滤过字符串属性在查询中考更大)。所以,你不能存储的时间戳的是(将被转换为字符串和你将无法对它们进行比较),你可以不设置时间戳为的DateTime 出于同样的原因。 而你只是无法存储日期,也没有日历对象。

This is important because you can't do all the comparisons with Strings. It is only allowed the equality (and ne) comparison (you can't test a greater than filter over a String property in your queries). So you can't store timestamps as Long (will be converted to String and you won't be able to compare them) and you can't set timestamps as DateTime for the same reason. And you just can't store Date nor Calendar objects.

反正每个 CloudEntity 有两个的DateTime 调性质默认 CloudEntity.PROP_CREATED_AT CloudEntity.PROP_UPDATED_AT 。您可以设置查询过滤器这一领域。要做到这一点,你需要设置过滤器

Anyway every CloudEntity has two DateTime porperties by default CloudEntity.PROP_CREATED_AT and CloudEntity.PROP_UPDATED_AT. You can set a query filter with this fields. To do so you need to set the filter as

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));

您的需要使用 的DateTime 进行比较。如果你是courious,您可以不会使用日期而不是的DateTime 这个比较。你会得到这个错误:

You need to use a DateTime for the comparison. If you are courious, you can NOT use Date instead of DateTime for this comparation. You would get this error:

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."
...

其他奇怪的是,aparently,您不可以日期时间=新的日期时间比较(0)(即1970-01- 01T01:00:00.000 + 01:00)作为后端收到查询为:

Other weird thing is that, aparently, you can not do comparisons with dateTime = new DateTime(0) (i.e. 1970-01-01T01:00:00.000+01:00) as the backend receives the query as:

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

不会给任何错误,但将返回任何列表:结果:空。看起来它把比较为字符串。如果你使用 DateTime的日期时间=新的日期时间(1)相反,你将发送一个查询为:

Won't give any error but will return nothing list: result: null. Looks like it treats the comparison as a String. If you use DateTime dateTime = new DateTime(1) instead, you will send a query as:

list: executing query: {filterDto={operator=GT, values=[_updatedAt, 1970-01-01T01:00:00.001+01:00]},

看上去和以前一样,但在后端将执行查询为:

looks the same as before but the backend will execute the query as:

 query: (_kindName:"Data") AND ( _updatedAt > 1 ), schema: {_kindName=STRING, _updatedAt=DOUBLE}

当我看到一个 DOUBLE 我试着提交双击代替日期时间,但它不工作(没有错误,但没有结果)。

As I see a DOUBLE I tried submit a Double instead a DateTime, but it doesn't work (no error but not result).

但是,好消息大家好!我们可以做的比较的 整数为:

Integer anInteger = Integer.valueOf(0);
myQuery.setFilter(F.gt(CloudEntity.PROP_UPDATED_AT, anInteger));

在后台看到:

the backend sees:

query: (_kindName:"Data") AND ( _updatedAt > 0 ), schema: {_kindName=STRING, _updatedAt=INT32}

和我们将得到的预期值(因为1970-01-01T01所有实体更新时间:00:00.000 + 01:00)

and we'll get the expected value (all the entities updated since 1970-01-01T01:00:00.000+01:00)

对不起,我的错误(我的英语不是很好),我希望这能帮助别人的同时,至少救了他一些时间。

Sorry for my mistakes (my english isn't good) and I hope this can help someone and, at least, save him some time.

这篇关于发送和从数据存储retreaving数据与移动后端启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆