我是Android新手并且一直在尝试使用Parse.com。他们网站上提供的代码片段非常有用。我已经能够将数据从我的应用程序发送到数据库,这意味着它正在连接到正确的数据库。但是我无法从中检索到正确的数据。无论我使用什么对象ID,它总是返回null或0(取决于它是字符串还是整数)。我的代码如下:
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());
}
}
});
}
}
我已尝试将[ query.whereEqualto ]
与playerName
一起使用并获得分数。相同的结果。
我也试过这个:
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());
}
}
返回的值为0或null。
数据库如下所示:
答案 0 :(得分:2)
您正在使用错误的对象进行检索。 试试这个。
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());
}
}
我建议使用子类进行解析。 http://blog.parse.com/2013/05/30/parse-on-android-just-got-classier/