解析“没有找到查询结果(代码101)”。无法获取对象ID

时间:2016-02-05 19:53:41

标签: java android parse-server

我已经在我的桌面上安装了示例解析服务器(https://github.com/ParsePlatform/parse-server-example)并制作了一个简单的应用程序来测试它。

我的应用将对象保存到服务器,获取对象并将mTextView的值设置为我的对象的值。

问题是,当我尝试使用此代码从服务器获取数据时,它可以工作:

    query.getInBackground("5K7N8a8Dmd", new GetCallback<ParseObject>() ...

(用curl获取对象id)

但是当我尝试这个时(使用curl获取对象id):

            gameScore.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                if (e == null) {
                    objectId = gameScore.getObjectId();
                } else {
                    Log.e("saveInBackground", getErrorMessage(e));
                }
            }
        });

...

    query.getInBackground(objectId, new GetCallback<ParseObject>() ...

它不起作用。

logcat的:

E/getInBackground﹕ no results found for query - code: 101

MainActivity.java

    public class MainActivity extends Activity {

    public TextView mTextView;
    public String objectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.text_view);

        Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
                        .applicationId(Constants.APP_ID)
                        .server(Constants.SERVER_URL)
                        .build()
        );

        final ParseObject gameScore = new ParseObject("Foo1234");
        gameScore.put("score", 5000);
        gameScore.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                if (e == null) {
                    objectId = gameScore.getObjectId();
                } else {
                    Log.e("saveInBackground", getErrorMessage(e));
                }
            }
        });

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Foo1234");
        query.getInBackground(objectId, new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    mTextView.setText(Integer.toString(gameScore.getInt("score")));
                } else {
                    Log.e("getInBackground", getErrorMessage(e));
                }
            }
        });
    }

    public String getErrorMessage(ParseException e) {
        return e.getMessage() + " - code: " + e.getCode();
    }
}

Constants.java

    public class Constants {
    public static String SERVER_URL = "http://192.168.1.14:1337/parse/";
    public static String APP_ID = "myAppId";
}

提前致谢。

2 个答案:

答案 0 :(得分:1)

试试这个

ParseQuery<ParseObject> query = ParseQuery.getQuery("_Foo1234");
query.whereEqualTo("objectId","wfBB0gpCkP");
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> objects, ParseException e) {
        if (e == null) {
            // row of Object Id "wfBB0gpCkP"
        } else {
            // error
        }
    }
});

同时更改

ParseQuery<ParseObject> query = ParseQuery.getQuery("Foo1234");

ParseQuery<ParseObject> query = ParseQuery.getQuery("_Foo1234"); 

看看是否有效。

答案 1 :(得分:0)

问题是saveInBackground()的“背景”部分。保存后的行上的保存未完成。事实上,它甚至还没有开始拯救。在完成处理程序运行之前,保存尚未完成。

考虑...

        // code here runs **first**
        gameScore.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            // code here runs **third, much later than second**
            if (e == null) {
                objectId = gameScore.getObjectId();
            } else {
                Log.e("saveInBackground", getErrorMessage(e));
            }
        }
        // code here runs **second, immediately**

在保存的完成处理程序中运行“get”查询,您将看到它的工作原理。 (当然,没有太多理由获得该对象,因为您拥有刚刚调用的对象的句柄保存)。