android - 无法根据链接接收对象

标签 android mongodb datastore kinvey

按照我创建对象的文档中的说明,我填充它们的属性并将链接保存在数据存储中:

public class InvitationEntity extends GenericJson {
    @Key
    public String objectId;
    @Key
    public int status;
    @Key("_id")
    public String id;
    @Key
    public String name;
}
public class EventEntity extends GenericJson {
    @Key
    public String name;
    @Key
    public String eventDate;
    @Key
    public String location;
    @Key
    public String tip;
    @Key
    public KinveyReference invitations;

    public void initReference(InvitationEntity myInvitation){
        KinveyReference reference = new KinveyReference("invitationCollection", myInvitation.get("_id").toString());
        this.invitations = reference;
    }
}

...

 InvitationEntity invitationEntity = new InvitationEntity();
 invitationEntity.name = "3";
 invitationEntity.objectId="3";
mKinveyClient.appData("invitationCollection", InvitationEntity.class).save(invitationEntity, new KinveyClientCallback<InvitationEntity>() {
                @Override
                public void onSuccess(InvitationEntity invitationEntity1) {
                    Log.e("my", "Ok1 " + invitationEntity1.toString());

                    EventEntity eventEntity = new EventEntity();
                    eventEntity.tip = "33";
                    eventEntity.initReference(invitationEntity1);
                    mKinveyClient.appData("eventCollection", EventEntity.class).save(eventEntity, new KinveyClientCallback<EventEntity>() {
                        @Override
                        public void onSuccess(EventEntity eventEntity_rez) {
                            Log.e("my", "Ok2 " + eventEntity_rez.toString());
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            Log.e("my", "Failed to save entity " + t.getMessage());
                        }
                    });
                }

                @Override
                public void onFailure(Throwable t) {
                    Log.e("my", "Failed to save entity " + t.getMessage());
                }
            });

现在我请求:

    Query q = mKinveyClient.query();
    mKinveyClient.appData("eventCollection", EventEntity .class).get(q, new KinveyListCallback<EventEntity >() {
        @Override
        public void onSuccess(EventEntity [] resalt) {
                Log.d("my", resalt[0].toString());
        }

        @Override
        public void onFailure(Throwable t) {
            Log.d("my", "Error fetching data: " + t.getMessage());
        }
    });

我收到结果:

{"invitations":{"_collection":"invitationCollection",
                            "_id":"52715e5b13dde23677021c80",
                            "_type":"KinveyRef"},
"tip":"33",
"_acl":{"creator":"526182566bbfcbf429000518"},
"_kmd":{"lmt":"2013-10-30T19:30:36.086Z",
                 "ect":"2013-10-30T19:30:36.086Z"},
"_id":"52715e5c13dde23677021c81",
"kid":"kid_TTpvqRShiO",
"collection":"eventCollection"}

并且希望收到:

{"invitations":{"_id":"52715e5b13dde23677021c80",
                             "name":"3",
                             "objectId":"3",
                             "status":0,
                             "_acl":{"creator":"526182566bbfcbf429000518"},
                             "_kmd":{"lmt":"2013-10-30T19:30:35.912Z",
                                               "ect":"2013-10-30T19:30:35.912Z"},
                                               "kid":"kid_TTpvqRShiO",
                                               "collection":"invitationCollection"},
"tip":"33",
"_acl":{"creator":"526182566bbfcbf429000518"},
"_kmd":{"lmt":"2013-10-30T19:30:36.086Z",
                 "ect":"2013-10-30T19:30:36.086Z"},
"_id":"52715e5c13dde23677021c81",
"kid":"kid_TTpvqRShiO",
"collection":"eventCollection"}

如何接收对象而不是其中的链接有其他对象?

最佳答案

我是 Kinvey 的一名工程师,可以帮助您解决这个问题——我在我们的 support forums 上发布了相同的答案。 ,但认为其他人可能会在这里偶然发现它:

这并非不可能!

在 Appdata 上,Get 和 GetEntity 方法有多种变体。在此处查看 javadoc:http://devcenter.kinvey.com/android/reference/api/reference/com/kinvey/android/AsyncAppData.html

要解析 KinveyReferences,将包含您希望解析的字段名称的 String[] 作为第二个参数传递给 get 方法,在您的情况下您想要使用:

mKinveyClient.appData("eventCollection", EventEntity .class).get(q, new String[]{"invitations"}, new KinveyListCallback() { ... }

现在,onSuccess 回调将返回一个 EventEntity,但引用已解析,您可以调用:

InvitationEntity myInvite = results[0].getInvitations.getTypedObject(InvitationEntity.class);

关于android - 无法根据链接接收对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19693399/

相关文章:

java - 更新 APIGEE 实体时出错

java - 如何在android studio中使用socket.io加载 map 后添加新标记

android - 有什么办法可以阻止 android 中键盘出现的标签吗?

javascript - Mongo : Is it a good practice to do collection. find() 返回主节点上的所有项目

android - 未显示自定义警报对话框

mongodb - 使用 $in 排序不返回所有文档

node.js - Mongoose 计算嵌入文档数组中的某些元素

java - 如何从 GAE java blobstore 上传获取文件名

python - 运行 Google App Engine Python 的本地数据存储是否有任何限制?

java - 库 appengine.api.datastore 和 com.google.cloud.datastore 之间有什么区别?