Android Parse.com查询关系

时间:2015-06-24 21:27:48

标签: android parse-platform

我有一个自定义的Employee表和自定义的Organization表。 Employee表有一列Relation<组织>。 我想查询员工对象的组织。

// I have an employee object
ParseObject employee = ParseUser.getCurrentUser().getParseObject("employee");
// I get relation query
ParseQuery<ParseObject> query = employee.getRelation("organizations").getQuery();
// I execute query like this
query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            // code here
        });

在我的回调中,我得到了ClassCastException:

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject
        at com.parse.ParseRESTQueryCommand.constructParameters(ParseRESTQueryCommand.java:84)
        at com.parse.ParseRESTQueryCommand.findCommand(ParseRESTQueryCommand.java:15)
        at com.parse.ParseDefaultQueryController.findFromNetworkAsync(ParseDefaultQueryController.java:57)
        at com.parse.ParseDefaultQueryController.access$100(ParseDefaultQueryController.java:17)
        at com.parse.ParseDefaultQueryController$6.runOnNetworkAsync(ParseDefaultQueryController.java:193)
        at com.parse.ParseDefaultQueryController.runCommandWithPolicyAsync(ParseDefaultQueryController.java:292)
        at com.parse.ParseDefaultQueryController.findWithCachePolicyAsync(ParseDefaultQueryController.java:201)
        at com.parse.ParseDefaultQueryController.findAsync(ParseDefaultQueryController.java:28)
        at com.parse.ParseQuery.findAsync(ParseQuery.java:1187)
        at com.parse.ParseQuery$2$1.then(ParseQuery.java:1129)
        at com.parse.ParseQuery$2$1.then(ParseQuery.java:1125)
        at bolts.Task$14.run(Task.java:796)
        at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
        at bolts.Task.completeAfterTask(Task.java:787)
        at bolts.Task.continueWithTask(Task.java:599)
        at bolts.Task.continueWithTask(Task.java:610)
        at bolts.Task$12.then(Task.java:702)
        at bolts.Task$12.then(Task.java:690)
        at bolts.Task$14.run(Task.java:796)
        at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
        at bolts.Task.completeAfterTask(Task.java:787)
        at bolts.Task.continueWithTask(Task.java:599)
        at bolts.Task.continueWithTask(Task.java:574)
        at bolts.Task.onSuccessTask(Task.java:690)
        at bolts.Task.onSuccessTask(Task.java:680)
        at bolts.Task.onSuccessTask(Task.java:714)
        at com.parse.ParseQuery$2.call(ParseQuery.java:1125)
        at com.parse.ParseQuery$2.call(ParseQuery.java:1122)
        at com.parse.ParseQuery.doWithRunningCheck(ParseQuery.java:1091)
        at com.parse.ParseQuery.findInBackground(ParseQuery.java:1122)

我做错了什么?这与Parse教程(https://www.parse.com/docs/android/guide#relations-using-parse-relations)完全相同。我在Google或SOF上找不到任何答案。 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

来自@cYrixmorten的评论

ParseUser.getCurrentUser().getParseObject("employee")默认情况下不会返回employee对象,只返回指针。 在致电fetch()之前getParseObject()您的用户应该有效。

如果切换到使用指针数组而不是关系,则可以执行ParseQuery.getQuery("Employee").get("CITLvyOFuK").include("organizations")之类的操作。返回对象将包含一个Employee对象,其中组织列表中填充了数据,例如employee.getList("organizations").get(0).getString("name")可以使用。

如果您期望很多(100+)指针,那么关系就很棒。这样就避免了为每个员工提取大量数据。在你的情况下,我认为没有理由不简单地使用一系列组织。

以及我的说明

如果要在Parse Data Browser中添加指针数组,请使用以下模式:

[{"__type":"Pointer","className":"CLASSNAME","objectId":"OBJECTID"},{"__type":"Pointer","className":"CLASSNAME","objectId":"OBJECTID"}]

答案 1 :(得分:0)

here's how you do it. survey being the header record with hearaboutusID as the key field in survey linking to hearaboutus table which is a relation.

    ParseQuery query = new ParseQuery("survey");
    try {
        query.include("hearaboutus");
        poSurvey = query.get(<objectID>);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    ParseQuery<ParseObject> qry = poSurvey.getRelation("hearaboutusID").getQuery();
    qry.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> hearList, ParseException e) {
            for(ParseObject hear : hearList){                   
                Log.d("HEAR ABOUT US ITEM: ", hear.getObjectId());
            }
        }
    });

答案 2 :(得分:-1)

https://www.dropbox.com/s/b4h2to1cdbx087b/SAmpleApp.zip?dl=0尝试这个我已经完成类似的几个星期回来了整个src代码,你可能需要很少的库来运行它....希望这会有所帮助。