android parse, how to find other username? - android

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", "female");
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, com.parse.ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Username Found",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Username Not Found",
Toast.LENGTH_LONG).show();
}
}
});'
I have problem with these code, i want to search other username, please help me.
I already write the code but always get in "Username found" but in database there no name like that.

You are wrong because, into findInBackground callback, you currently check only the e variable (if it is null or not) and not the objects size. Exception is alwasy null if the query return a valid result from Cloud Code (in this case the object is not null and it size is 0 or greather than); exception variable is valorized only if the execution of query raise an exception/error.
To check if a query returns a records or not you must to check the size of objects (List):
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", "female");
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, com.parse.ParseException e) {
if (e == null) {
if (objects.size() > 0) {
Toast.makeText(getApplicationContext(), "Username Found",
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Username Not Found",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "An error has occurred!",
Toast.LENGTH_LONG).show();
}
}
});
Take in consideration the opportunity to limit the query to 1 result (if you looking for one specific record) or use getFirstInBackground method to improve query performance.

Related

How to pass Stripe token from Android to ParseServer?

Welcome all!
I am currently working to pass a token generated by Stripes API from an Android app to a ParseServer. Below is my code, please be advised that I commented out previous failed attempts to let you know what I have tried and to also spark your imagination. Please note that with trial and error the issue presents to be with saving the data to the server. I have double checked that the class User has write permissions and it has an Object attribute titled token.
// Test the data.
if (userCard.validateCard()) {
Stripe stripe = new Stripe(CardActivity.this, "correct data is here I removed it, for StackOverflow");
stripe.createToken(
userCard,
new TokenCallback() {
public void onSuccess(final Token token) {
// Send token to your server
// Query the users, and get the username.
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
ParseUser user = ParseUser.getCurrentUser();
String objectId = user.getObjectId();
// Query the current user.
//query.whereEqualTo("objectId", username);
ParseObject object;
try {
object = query.get(objectId);
object.put("token", token);
object.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(CardActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
}
});
} catch (ParseException e) {
Toast.makeText(CardActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
// Attempt to update... Currently not working.
/*query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null && objects != null) {
for (ParseObject object : objects) {
object.put("token", token);
object.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(CardActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
}
});
}
} else {
Toast.makeText(CardActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});*/
}
public void onError(Exception error) {
// Show error message
Toast.makeText(CardActivity.this,
error.getMessage(),
Toast.LENGTH_LONG
).show();
}
}
);
} else {
Toast.makeText(CardActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
A few things here:
1) Parse allows you to add/overwrite information to objects from their "shell". This is a ParseObject instance, set to a specified class, assigned an objectId, and then whatever values you want to add/change, and then saved. Any field you did not assign a value to will be ignored, so say you only set field3, field2 and field1 will not be overwritten to nothing.
2) ParseUser user = ParseUser.getCurrentUser() already returns a user object. You don't even need to create a shell from the id, this is a fully functioning parse object. You can then set the value you want and save it!
3) Queries and fetches (and cloud function calls) are asynchronous. This means that the code executes over time on a background thread, and your main thread will go on. So, things that require the results of these methods need to be called within the completion handler. You're doing object = query.get(objectId), but query.get() takes a bit to run so you're probably running through the rest of the code block before object has a proper value.
4) To my knowledge (not an Android developer, but I've used the JS and iOS SDKs) the Parse SDKs have a specific query for the User class that is a bit easier and safer to use than creating a ParseQuery set to the "User" class. Should be something like ParseUser.query()
So, not being an Android developer, I think what you want is more like this:
// Test the data.
if (userCard.validateCard()) {
Stripe stripe = new Stripe(CardActivity.this, "correct data is here I removed it, for StackOverflow");
stripe.createToken(
userCard,
new TokenCallback() {
public void onSuccess(final Token token) {
// Send token to your server
// Query the users, and get the username.
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
ParseUser user = ParseUser.getCurrentUser();
try {
user.put("token", token);
user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(CardActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
}
});
} catch (ParseException e) {
Toast.makeText(CardActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public void onError(Exception error) {
// Show error message
Toast.makeText(CardActivity.this,
error.getMessage(),
Toast.LENGTH_LONG
).show();
}
}
);
} else {
Toast.makeText(CardActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}

How to check if Username/Email already exists android parse

I've searched everywhere but haven't found anything about how to check if a username/email address in the parse database is taken. I've found some answers on the internet on parse.com forums but they weren't clear.
Thanks if you could help.
If this has an answer somewhere, then please comment instead of marking so I can delete it.
I think this will do what you need if I understand your question correctly:
final ParseQuery<ParseUser> emailQuery = ParseUser.getQuery();
emailQuery.whereEqualTo("email", emailAddress);
final ParseQuery<ParseUser> usernameQuery = ParseUser.getQuery();
usernameQuery.whereEqualTo("email", username);
List<ParseQuery> queries = new ArrayList<>();
queries.add(emailQuery);
queries.add(usernameQuery);
final ParseQuery<ParseUser> query = ParseQuery.or(queries);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> results, ParseException e) {
// results has the list of users that match either the email address or username
}
});
https://www.parse.com/docs/android/guide#queries-compound-queries
Or you could do it this way:
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// yippee!!
} else {
switch (e.getCode()) {
case ParseException.USERNAME_TAKEN: {
// report error
break;
}
case ParseException.EMAIL_TAKEN: {
// report error
break;
}
default: {
// Something else went wrong
}
}
}
}
});
Try this to check your data in parse. I hope this will helpful for you.
// this is create query of your parse table
ParseQuery<ParseObject> query = new ParseQuery<>(your prars tableName);
query.whereEqualTo(YourParseColumeNameEmail, yourEmail);
query.whereEqualTo(YourParseColumeNamePassword, yourPassword);
// this is for doing in background
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
if (scoreList.size() == 0) {
// if there is no data like your email and password then it,s come here
} else {
// if there is data like your email and password then it,s come here
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});

Retrieve data from a class in Parse.com

I have a class in Parse.com, as XYZ, and it has columns email, password, phone, age. I have email and password combination and I want to check whether there is such a combination in the class? How do I do this?
I have a class Donate, and I have been given an email(e) and password(pw). Here goes the code:
`ParseQuery<ParseObject> query = ParseQuery.getQuery("donate");
query.whereEqualTo("email", e);
query.whereEqualTo("password", pw);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Success!!!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Something went wrong!!!", Toast.LENGTH_LONG).show();
}
}
}); `
And this always shows 'success' irrespective of the strings e, and pw.
ParseQuery return ParseException just if there was technical issue calling the server.
else, even though your query doesn't find exactly what you looked for,
it will return an empty list and the 'e' var will be null.
so what you need to do is :
ParseQuery<ParseObject> query = ParseQuery.getQuery("donate");
query.whereEqualTo("email", e);
query.whereEqualTo("password", pw);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
if (list.isEmpty() == false) { // add this
Toast.makeText(getApplicationContext(), "Success!!!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Worng password!!!", Toast.LENGTH_LONG).show(); // add this
}
} else {
Toast.makeText(getApplicationContext(), "Something went wrong!!!", Toast.LENGTH_LONG).show();
}
}
});

cannnot save the result of parse query in an android variabe

I am currently using Parse.com's API for an android application. I want to retreive an array from parse and save it as a list to work with later. but tha problem is when i'm out of the query my list turnes empty. can u help me?
ParseUser currentUser = ParseUser.getCurrentUser();
String userId= currentUser.getObjectId();
final ParseQuery<ParseObject> pQuery = ParseQuery.getQuery("Friendship");
pQuery.whereEqualTo("user", userId);
pQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e==null) {
if (list.size()>0) {
ParseObject p = list.get(0);
if (p.getList("friendsList")!=null) {
list11 = p.getList("friendsList");
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+ list11.get(2).toString(),
Toast.LENGTH_LONG).show();
}
}
}
}
});
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+list11.get(1).toString(),
Toast.LENGTH_LONG).show();
the first Toast is working but the second one gives me a NullPointerException.
You can only put second Toast in done method like this:-
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e==null) {
if (list.size()>0) {
ParseObject p = list.get(0);
if (p.getList("friendsList")!=null) {
list11 = p.getList("friendsList");
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+ list11.get(2).toString(),
Toast.LENGTH_LONG).show();
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+list11.get(1).toString(),
Toast.LENGTH_LONG).show();
}
}
}
}
Because findInBackground method start a new background thread.when you show second toast then list11 may be empty. Due to this it throws a NullPointerException

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

Categories

Resources