gpt4 book ai didi

java - Mobile Backend Starter subscribeToCloudMessage 将不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:50 26 4
gpt4 key购买 nike

当我将 subscribeToCloudMessage() 函数与 CloudBackendMessaging.TOPIC_ID_BROADCAST 一起用作 topicId 时,就像在 CloudBackendFragment.java 中所做的那样,一切正常,但是当我为该函数提供我自己的字符串时,我收到此消息:

错误:

m.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request

"code": 400,
"errors": [
{
"domain": "global",
"message": "SubscriptionIDs: String properties must be 500 characters or less. Instead, use com.google.appengine.api.datastore.Text, which can store strings of any length.",
"reason": "badRequest"
}
],
"message": "SubscriptionIDs: String properties must be 500 characters or less. Instead, use com.google.appengine.api.datastore.Text, which can store strings of any length."

at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:111)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:38)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1042)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at com.google.cloud.backend.core.CloudBackend.list(CloudBackend.java:314)
at com.google.cloud.backend.core.CloudBackendAsync.access$8(CloudBackendAsync.java:1)
at com.google.cloud.backend.core.CloudBackendAsync$9.callBackend(CloudBackendAsync.java:270)
at com.google.cloud.backend.core.CloudBackendAsync$9.callBackend(CloudBackendAsync.java:1)
at com.google.cloud.backend.core.CloudBackendAsync$BackendCaller.run(CloudBackendAsync.java:402)

最佳答案

此错误消息表示属性 SubscriptionIDs(您可以在数据存储中的 _DeviceSubscription 类型下找到它)值超过 500 个 Unicode 字符限制。阅读docs供引用。

来自文档:

For text strings and unencoded binary data (byte strings), the Datastore supports two value types:

  1. Short strings (up to 500 Unicode characters or bytes) are indexed and can be used in query filter conditions and sort orders.
  2. Longstrings (up to 1 megabyte) are not indexed and cannot be used in query filters and sort orders.

发生这种情况的原因是 MBS 试图将所有订阅写入一个属性。

因此,为了克服这个问题,我们需要使用 Text 而不是 String 输入 MBS backend source code对于 SubscriptionIDs 属性。为此,您需要在 DeviceSubscription.java 类中进行以下更改:

要设置 Text 属性,请替换这行代码:

deviceSubscription.setProperty(PROPERTY_SUBSCRIPTION_IDS, this.gson.toJson(subscriptions));

用这一行:

deviceSubscription.setProperty(PROPERTY_SUBSCRIPTION_IDS, new Text(this.gson.toJson(subscriptions)));

从数据存储中获取 Text 属性:

  1. 替换这一行:

    String subscriptionString = (String) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);

    用这个:

    Text text = (Text) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);
    String subscriptionString = text.getValue();
  2. 替换这一行:

    String ids = (String) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);

    用这个:

    Text text = (Text) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);
    String ids = text.getValue();
  3. 替换这一行:

    String[] ids = new Gson().fromJson((String) entity.getProperty(PROPERTY_SUBSCRIPTION_IDS),
    String[].class);

    用这个:

    Text text = (Text) entity.getProperty(PROPERTY_SUBSCRIPTION_IDS);
    String[] ids = new Gson().fromJson(text.getValue(), String[].class);

似乎是有效的解决方案。到目前为止,我没有注意到对我的项目的负面影响。

注意:正如文档所述,文本 未编入索引,不能用于查询过滤器和排序顺序。因此,如果需要为属性 SubscriptionIDs 编制索引,此限制可能会导致其他问题。

关于java - Mobile Backend Starter subscribeToCloudMessage 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com