3

我已经在我的桌面上安装了示例解析服务器 ( 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>() ...

它不起作用。

日志猫:

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

常量.java

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

提前致谢。

4

2 回答 2

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

看看这是否有效。

于 2016-02-05T21:02:28.880 回答
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”查询,您会看到它有效。(当然,没有太多理由获取该对象,因为您拥有刚刚调用 save 的对象的句柄)。

于 2016-02-05T22:20:27.153 回答