Parse getInBackground not Working (User Querying with ObjectID) - android

I am currently working on the User Profile part of my app and I would like to run a query on a specific user object using the objectId of that user. With that query, I hope to get the users info(email, username, hometwown, etc.) from parse and display it on the page.
The parse documentation recommends the getInBackground method like so:
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
query.getInBackground(myId, new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
objectWasRetrievedSuccessfully(object);
} else {
objectRetrievalFailed();
}
}
}
Using this I would think I would replace "MyClass" with "User" since I will be searching the user class for the specific Id entered. However, I am getting an exception every time and the query is failing. A copy of my code is below where, as an example, I attempt to extract the "hometown" attribute from the specefic user and set it to display as a textview. mId is equal to the specific object ID of the user passed over from another activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends_profile);
mHometownField = (TextView) findViewById(R.id.textView);
mId = getIntent().getStringExtra(ParseConstants.KEY_ID);
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.getInBackground(mId, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, com.parse.ParseException e) {
if (e == null) {
mHometown = parseObject.get(ParseConstants.KEY_HOMETOWN).toString();
}
else {
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(FriendsProfileActivity.this);
builder.setTitle(R.string.error_title)
.setMessage(e.getMessage())
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
mHometownField.setText(mHometown);
}

A very common mistake. The "User" class is an internal class so you have to query it differently:
ParseQuery<ParseUser> query = ParseUser.getQuery();
Same applies for the "Role" and "Installation" classes.

Related

How can I call the name of a user if the mail is from esuario from parse server in Android?

In summary, if the mail is registered, how can I show the name of that user from Android with parse server.
Help please, this is for a college project.
In this function it already tells me that the email is registered, but I need to know the user of that email and show it in a TextView or with a log.d().
ParseQuery<ParseObject> query = ParseQuery.getQuery("init_app");
query.whereEqualTo("email", userdialog.getText().toString());
query.getFirstInBackground(new GetCallback<ParseObject>()
{
public void done(ParseObject object, ParseException e)
{
if (object == null)
{
Toast.makeText(initapp.this,"Are not you a registered user",Toast.LENGTH_LONG).show();
return;
}
else
{
}
}
});
If you use a query, for user search match with this email,in your callback this object 'object' represent this user,and now you:
ParseQuery<ParseObject> query = ParseQuery.getQuery("init_app");
query.whereEqualTo("email", userdialog.getText().toString());
query.getFirstInBackground(new GetCallback<ParseObject>(){
public void done(ParseObject object, ParseException e){
if (object == null || e!=null){
Toast.makeText(initapp.this,"Are not you a registered user",Toast.LENGTH_LONG).show();
return;
}else{//and here
//you can get args in this object
//object.get("name");
//object.get("age");
//whatever
//and
textView.setText(object.get("name"))
}
}
});

Android: Updating fields for ParseUsers

I am using ParseUser for user management. Having the username (unique), I planned to query the User class to identify the user and then put a new score to the "score" field, just like a Leaderboard. Coded as follows:
Code:
public void update_user_score(String username, int original_score, int add)
{
final int new_score = original_score + add;
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", username);
query.setLimit(1);
query.findInBackground(new FindCallback()
{
public void done(final ParseObject object, ParseException e)
{
if (e == null)
{
object.saveInBackground(new SaveCallback()
{
public void done(ParseException e)
{
object.put("score", new_score);
object.saveInBackground();
onBackPressed();
}
});
}
}
});
}
Situation:
It states that The type new FindCallback(){} must implement the inherited abstract method FindCallback.done(List, ParseException) . I have tried that FindCallback.done and found it unsucessful.
Question:
I would like to ask how the other fields of the user be updated with the username given? Thanks!
As per the parse documentation said,
The ParseUser class is secured by default. Data stored in a ParseUser can only be modified by that user. By default, the data can still be read by any client. Thus, some ParseUser objects are authenticated and can be modified, whereas others are read-only.
Specifically, you are not able to invoke any of the save or delete type methods unless the ParseUser was obtained using an authenticated method, like logIn or signUp. This ensures that only the user can alter their own data.
The following illustrates this security policy:
ParseUser user = ParseUser.logIn("my_username", "my_password");
user.setUsername("my_new_username"); // attempt to change username
user.saveInBackground(); // This succeeds, since the user was authenticated on the device
// Get the user from a non-authenticated manner
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getInBackground(user.getObjectId(), new GetCallback<ParseUser>() {
public void done(ParseUser object, ParseException e) {
object.setUsername("another_username");
// This will throw an exception, since the ParseUser is not authenticated
object.saveInBackground();
}
});
please go through this link Reference
public void update_user_score(String username, int original_score, int add)
{
final int new_score = original_score + add;
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", username);
query.setLimit(1);
query.findInBackground(new FindCallback()
{
public void done(final ParseUser object, ParseException e)
{
if (e == null)
{
object.put("score", new_score);
object.saveInBackground(new SaveCallback()
{
public void done(ParseException e)
{
if(e==null)
onBackPressed();
}
});
}
}
});
}
As far as I understand from your question is; you want to find the user entry in user table via using the specified user id and update the score field. First suggestion, instead of findInBackground use getFirstInBackground. Following that check the returned user object if not null perfrom the user update operation. Then call saveInBackground. saveInBackground is used for perfroming actions when the save is succesful. In your code, I see that you try to save the object in done method again. Also, one suggestion is that control the ACL.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getFirstInBackground(someUserId, new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException e) {
if (e == null) {
// No error e is null
// check user object
if (user == null) {
// no user , error
} else {
// perfrom save operation
// put score field
// call save in background
// in done method update UI for example.
}
} else {
// Something went wrong.
}
}
});

Query particular Parse user's information?

I have the objectID of a particular user (who is not the current user) and would like to find his Display Name, which is a custom field that I created.
As per Parse's guide, this code will create a list of users that match some particular attributes.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("gender", "female");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
} else {
// Something went wrong.
}
}
});
There's no reason for me to make a list, however, if only one user has that objectID. Any way for me to just get the display name of a particular user?
What you want instead is getFirstInBackground(), e.g.:
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getFirstInBackground(someUserId, new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException e) {
if (e == null) {
// The query was successful.
// check if we got a match
if (user == null) {
// no matching user!
} else {
// great, get the name etc
}
} else {
// Something went wrong.
}
}
});

how to find a user in a ParseUser object

Hi I was wondering why this code does not work. I am trying to query a ParseUser's username field to find a certain user but it keeps saying that it cant find it.
private void findUserName(String user) {
// query the User database to find the passed in user
ParseQuery query = ParseUser.getQuery();
query.whereEqualTo("username", user);
query.findInBackground(new FindCallback() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
foundUser = (objects.size() != 0);
}
});
}
Here is my method that calls it
if (!foundUser) {
errorMessage.setText("Invalid user name");
}
foundUser is a field because I couldnt return it in the method...
Parse treats User objects separate from Parse objects. You should use List<ParseUser> instead of List<ParseObject>. The Parse Android Guide provides an example https://parse.com/docs/android_guide#users-querying.
Here is the Parse example with your where clause.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", user);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
} else {
// Something went wrong.
}
}
});
İf you want to get current user this may be helpful;
String currentUser = ParseUser.getCurrentUser().getUsername();

getting ParseUser object

Hi I am using the Parse API's database for users and I was wondering how to get the actual ParseUser object so that I can add fields to it? Would I have to query to get the id first then retrieve the obejct that way? As in
ParseQuery query = new ParseQuery("GameScore");
query.getInBackground("xWMyZ4YEGZ", new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your game score
} else {
// something went wrong
}
}
});
Is there an easier way...
Do you mean the currently logged in user?
ParseUser currentUser = ParseUser.getCurrentUser();
Of do you have a relation to a user in another class?
If that is the case you can use ParseQuery.include to include that class in the response also.
ParseQuery query = new ParseQuery("GameScore");
query.include("User");
query.getInBackground("xWMyZ4YEGZ", new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your game score
ParseUser user = object.getParseObject("User"); // We have a user object
} else {
// something went wrong
}
}
});
Be aware for typos made here :)

Categories

Resources