简体   繁体   中英

How to retrieve data from parse.com database (Android)?

I'm new to Android and have been trying to use Parse.com. The code snippets provided on their website have been very useful. I've been able to send the data from my app to database, which means that it is connecting to the right database. But I'm unable to retrieve the correct data from it. No matter what object ID I use, it always returns either null or 0 (depending on if its a string or integer). My code is as follows :

public class MainActivity extends ActionBarActivity {



    TextView tv;
    Button b;
    String playerName;
    ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
    //int score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        tv=(TextView) findViewById (R.id.tv);
        b=(Button) findViewById (R.id.button1);
        Parse.initialize(this, "n12BWwZwk*************3dyKnuIUU", "vVi4TwI***************4peRD1VwyJ9CHw");
        final ParseObject gameScore = new ParseObject("GameScore");
       /* gameScore.put("score", 900);
        gameScore.put("playerName", "Pranav");
        gameScore.put("cheatMode", false);
        gameScore.saveInBackground();
      */


        query.whereEqualTo("objectId", "PnfRaUtHjB");
        query.findInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> arg0, ParseException arg1) {
                // TODO Auto-generated method stub
                if (arg1==null)
                {
                    int score=gameScore.getInt("score");
                    tv.setText("Player score - "+score);
                    Log.d("PTesting", "Player Score");
                    //Toast.makeText(getApplicationContext(), "Reached till Toast "+score,
                        //     Toast.LENGTH_LONG).show();

                }
                else
                {
                    Log.d("score", "Error: " + arg1.getMessage());
                }
            }

        });

}
}

I've tried using the [ query.whereEqualto ] with playerName and score too. Same results.

I've also tried this:

ParseQuery<ParseObject> query=ParseQuery.getQuery("GameScore");
        query.getInBackground("YsHLfJ7tUB", new GetCallback<ParseObject>() {

            @Override
            public void done(ParseObject arg0, ParseException arg1) {
                // TODO Auto-generated method stub

                if (arg1==null)
                {
                    int score = gameScore.getInt("score");
                    playerName = gameScore.getString("playerName");
                    boolean cheatMode = gameScore.getBoolean("cheatMode");
                    tv.setText("Updating "+playerName);

                }
                else
                {
                    Log.d("score", "Error: " + arg1.getMessage());
                }
            }

The values returned are either 0 or null.

The database looks like this:

http://i.imgur.com/SsUO8AG.jpg

you are using wrong object for retrieving. try this.

ParseQuery<ParseObject> query=ParseQuery.getQuery("GameScore");
        query.getInBackground("YsHLfJ7tUB", new GetCallback<ParseObject>() {

            @Override
            public void done(ParseObject arg0, ParseException arg1) {
                // TODO Auto-generated method stub

                if (arg1==null)
                {
                    int score = arg0.getInt("score");
                    playerName = arg0.getString("playerName");
                    boolean cheatMode = arg0.getBoolean("cheatMode");
                    tv.setText("Updating "+playerName);

                }
                else
                {
                    Log.d("score", "Error: " + arg1.getMessage());
                }
            }

and i will suggest use subclasses for parse. http://blog.parse.com/2013/05/30/parse-on-android-just-got-classier/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM